* casting types @ 2014-10-01 1:49 Stribor40 2014-10-01 2:06 ` Jeffrey Carter 0 siblings, 1 reply; 19+ messages in thread From: Stribor40 @ 2014-10-01 1:49 UTC (permalink / raw) I have problem understanding this explicit conversion of types.... var1: FLOAT; tmp := Float(x) / 20.0 + Float(y)/ 30.0 + Float(z)/40.0; return var1 - truncated type of var1 Example if var1 = 123.456 then return 123.456 - 123 which is 0.123 How would I cast var1 to become 123 but still be able to subtract it from var1 and keep float type? ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-01 1:49 casting types Stribor40 @ 2014-10-01 2:06 ` Jeffrey Carter 2014-10-01 2:13 ` Stribor40 2014-10-01 2:56 ` Stribor40 0 siblings, 2 replies; 19+ messages in thread From: Jeffrey Carter @ 2014-10-01 2:06 UTC (permalink / raw) On 09/30/2014 06:49 PM, Stribor40 wrote: > > How would I cast var1 to become 123 but still be able to subtract it from var1 and keep float type? You can't, since there is no casting in Ada. However, you might find the 'Truncation attribute function useful. http://www.adaic.org/resources/add_content/standards/12rm/html/RM-A-5-3.html -- Jeff Carter "Nobody expects the Spanish Inquisition!" Monty Python's Flying Circus 22 ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-01 2:06 ` Jeffrey Carter @ 2014-10-01 2:13 ` Stribor40 2014-10-01 2:56 ` Stribor40 1 sibling, 0 replies; 19+ messages in thread From: Stribor40 @ 2014-10-01 2:13 UTC (permalink / raw) On Tuesday, 30 September 2014 22:06:58 UTC-4, Jeffrey Carter wrote: > On 09/30/2014 06:49 PM, Stribor40 wrote: > > > > > > How would I cast var1 to become 123 but still be able to subtract it from var1 and keep float type? > > > > You can't, since there is no casting in Ada. However, you might find the > > 'Truncation attribute function useful. > > > > http://www.adaic.org/resources/add_content/standards/12rm/html/RM-A-5-3.html > > > > -- > > Jeff Carter > > "Nobody expects the Spanish Inquisition!" > > Monty Python's Flying Circus > > 22 Would you mind showing an example please... ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-01 2:06 ` Jeffrey Carter 2014-10-01 2:13 ` Stribor40 @ 2014-10-01 2:56 ` Stribor40 2014-10-01 13:18 ` Dennis Lee Bieber 2014-10-02 9:24 ` Brian Drummond 1 sibling, 2 replies; 19+ messages in thread From: Stribor40 @ 2014-10-01 2:56 UTC (permalink / raw) ok so i tried truncation attribute and that works just great. Now I want to output this float point to the screen and this is the way i did it....myFunction is myFunction that returns float.... Put(Item=>myFunction,Exp=>0,Aft=>5); which gives me for example something like this... 0.42997 where without using this function i would get for example 3.23684E-01..... Is there any other way to do this? ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-01 2:56 ` Stribor40 @ 2014-10-01 13:18 ` Dennis Lee Bieber 2014-10-01 13:55 ` Stribor40 2014-10-02 9:24 ` Brian Drummond 1 sibling, 1 reply; 19+ messages in thread From: Dennis Lee Bieber @ 2014-10-01 13:18 UTC (permalink / raw) On Tue, 30 Sep 2014 19:56:59 -0700 (PDT), Stribor40 <ikamzic@gmail.com> declaimed the following: >ok so i tried truncation attribute and that works just great. Now I want to output this float point to the screen and this is the way i did it....myFunction is myFunction that returns float.... > >Put(Item=>myFunction,Exp=>0,Aft=>5); > >which gives me for example something like this... 0.42997 where without using this function i would get for example 3.23684E-01..... > Unless your function is really corrupting things, I expect the second to be 4.2997E-01 Floating point values (in any programming language) do not behave as "real numbers"... One) they are stored as binary floats, not decimal, so don't expect exact conversions in/out. Two) they are stored as an exponent, and a mantissa of the significant binary value -- no 0.00001, rather 1.0E-5 [if I counted correctly]. So, yes, you do have to control the formatting for display purposes. Your original post isn't too clear either... >I have problem understanding this explicit conversion of types.... > > var1: FLOAT; > > tmp := Float(x) / 20.0 + Float(y)/ 30.0 + Float(z)/40.0; Do these serve any purpose? Neither tmp, x, y, z are declared or initialized > return var1 - truncated type of var1 > >Example if var1 = 123.456 then > return 123.456 - 123 > >which is 0.123 > 123.456 - 123 should leave 0.456 >How would I cast var1 to become 123 but still be able to subtract it from var1 and keep float type? >Is there any other way to do this? -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/ ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-01 13:18 ` Dennis Lee Bieber @ 2014-10-01 13:55 ` Stribor40 2014-10-01 14:23 ` G.B. ` (2 more replies) 0 siblings, 3 replies; 19+ messages in thread From: Stribor40 @ 2014-10-01 13:55 UTC (permalink / raw) I want to be able to represent numbers like 0.1234,-1,2345 and so on so thats why I chose float. Is there any other way i can represent numbers like that? Is there any other data type? ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-01 13:55 ` Stribor40 @ 2014-10-01 14:23 ` G.B. 2014-10-01 17:16 ` Stribor40 2014-10-01 18:11 ` Jeffrey Carter 2014-10-02 9:21 ` Brian Drummond 2 siblings, 1 reply; 19+ messages in thread From: G.B. @ 2014-10-01 14:23 UTC (permalink / raw) On 01.10.14 15:55, Stribor40 wrote: > I want to be able to represent numbers like 0.1234,-1,2345 and so on so thats why I chose float. Is there any other way i can represent numbers like that? Is there any other data type? > Are these rational numbers 0.1234, whole number -1, and whole number 2345, respectively? The first falls under either floating point types or decimal fixed point types, the second is of some integer type, as is the last one. You should be able to learn how you can define just the types that you need, by using really any text on Ada, no matter how old. Frequently, there is no need to use any predefined types. These may neither reflect your problem well, nor let you see how to use types. If you follow any tutorial text that you like, it will let you see what numeric types these are: type Part_No is range 1_000 .. 9_999; type Bits_16 is mod 2**16; type Temperature is delta 0.1 digits 4; type Flt is digits 6 range 0.0 .. 42_000_000.0; ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-01 14:23 ` G.B. @ 2014-10-01 17:16 ` Stribor40 2014-10-01 17:38 ` Dirk Heinrichs 0 siblings, 1 reply; 19+ messages in thread From: Stribor40 @ 2014-10-01 17:16 UTC (permalink / raw) i would like to represent all numbers in interval [0, 1] ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-01 17:16 ` Stribor40 @ 2014-10-01 17:38 ` Dirk Heinrichs 2014-10-02 1:47 ` Dennis Lee Bieber 0 siblings, 1 reply; 19+ messages in thread From: Dirk Heinrichs @ 2014-10-01 17:38 UTC (permalink / raw) Stribor40 wrote: > i would like to represent all numbers in interval [0, 1] Well, according to what I've learned in mathematics, you can't. Neither in real life, nor in any programming language. You need to define some constraint, like number of digits after the decimal point. Bye... Dirk -- Dirk Heinrichs <dirk.heinrichs@altum.de> Tel: +49 (0)2471 209385 | Mobil: +49 (0)176 34473913 GPG Public Key CB614542 | Jabber: dirk.heinrichs@altum.de Tox: heini@toxme.se Sichere Internetkommunikation: http://www.retroshare.org Privacy Handbuch: https://www.privacy-handbuch.de ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-01 17:38 ` Dirk Heinrichs @ 2014-10-02 1:47 ` Dennis Lee Bieber 0 siblings, 0 replies; 19+ messages in thread From: Dennis Lee Bieber @ 2014-10-02 1:47 UTC (permalink / raw) On Wed, 01 Oct 2014 19:38:22 +0200, Dirk Heinrichs <dirk.heinrichs@altum.de> declaimed the following: {responding to Stribor40} >Stribor40 wrote: > >> i would like to represent all numbers in interval [0, 1] > >Well, according to what I've learned in mathematics, you can't. Neither in >real life, nor in any programming language. You need to define some >constraint, like number of digits after the decimal point. > And on most machines these days -- you can expect the hardware to support single precision (32-bit) with ~7 significant digits, double precision (64-bit) with ~15 significant digits... And only within the floating point processor, maybe IEEE 80-bit (downconverted when the final result is returned to the user). Note that "significant digits" does not take into account the decimal point... 12300000.0, 1.23, 0.00000123 all have 3 significant digits if they are /floating point/ values (they are all 1.23E<some exponent>) Fixed point types are closer to integers with an implied decimal point. -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/ ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-01 13:55 ` Stribor40 2014-10-01 14:23 ` G.B. @ 2014-10-01 18:11 ` Jeffrey Carter 2014-10-02 9:21 ` Brian Drummond 2 siblings, 0 replies; 19+ messages in thread From: Jeffrey Carter @ 2014-10-01 18:11 UTC (permalink / raw) On 10/01/2014 06:55 AM, Stribor40 wrote: > Is there any other data type? In Ada, the question shouldn't be "Is there any other data type?" (or "Which language-defined type should I use?"), but rather "What type should I declare for these values?" Ada is unique among mainstream languages in allowing the developer to declare numeric types that reflect the problem domain, and McCormick found this to be part of the most important reason ("Modeling of scalar objects") why his students succeeded with Ada. http://www.crosstalkonline.org/storage/issue-archives/2000/200008/200008-McCormick.pdf -- Jeff Carter "I'm a kike, a yid, a heebie, a hook nose! I'm Kosher, Mum! I'm a Red Sea pedestrian, and proud of it!" Monty Python's Life of Brian 77 ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-01 13:55 ` Stribor40 2014-10-01 14:23 ` G.B. 2014-10-01 18:11 ` Jeffrey Carter @ 2014-10-02 9:21 ` Brian Drummond 2 siblings, 0 replies; 19+ messages in thread From: Brian Drummond @ 2014-10-02 9:21 UTC (permalink / raw) On Wed, 01 Oct 2014 06:55:31 -0700, Stribor40 wrote: > I want to be able to represent numbers like 0.1234,-1,2345 and so on so > thats why I chose float. Is there any other way i can represent numbers > like that? Is there any other data type? Yes, there is also fixed point, though I have a feeling this is a trivial answer because you haven't quite got to the real question. - Brian ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-01 2:56 ` Stribor40 2014-10-01 13:18 ` Dennis Lee Bieber @ 2014-10-02 9:24 ` Brian Drummond 2014-10-03 3:35 ` Stribor40 1 sibling, 1 reply; 19+ messages in thread From: Brian Drummond @ 2014-10-02 9:24 UTC (permalink / raw) On Tue, 30 Sep 2014 19:56:59 -0700, Stribor40 wrote: > ok so i tried truncation attribute and that works just great. Now I want > to output this float point to the screen and this is the way i did > it....myFunction is myFunction that returns float.... > > Put(Item=>myFunction,Exp=>0,Aft=>5); > > which gives me for example something like this... 0.42997 where without > using this function i would get for example 3.23684E-01..... > > Is there any other way to do this? At this point it's not clear what "do this" means or why the second example is a scientific notation representation of a completely different number. Put together the smallest complete compilable example of the problem you see, and it will help the discussion along wonderfully. - Brian ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-02 9:24 ` Brian Drummond @ 2014-10-03 3:35 ` Stribor40 2014-10-03 7:45 ` Björn Lundin 2014-10-03 8:29 ` Jacob Sparre Andersen 0 siblings, 2 replies; 19+ messages in thread From: Stribor40 @ 2014-10-03 3:35 UTC (permalink / raw) Ok basically what I want to do now is following... I am trying to print out my float to the screen by doing following.. Put(myFunction); Where myFunction generates number 9.24543E-01 which is output to the screen.. I can also output it like this Put(Item=>Float 'Truncation(120.00*myFunction),Exp=>0,Aft=>5); to output 81.00000 to the screen. Now I would like to save this 81.00000 as an integer to some variable. Can you please show me how to do this please ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-03 3:35 ` Stribor40 @ 2014-10-03 7:45 ` Björn Lundin 2014-10-03 8:29 ` Jacob Sparre Andersen 1 sibling, 0 replies; 19+ messages in thread From: Björn Lundin @ 2014-10-03 7:45 UTC (permalink / raw) On 2014-10-03 05:35, Stribor40 wrote: > Ok basically what I want to do now is following... > > I am trying to print out my float to the screen by doing following.. > > > Put(myFunction); > > Where myFunction generates number 9.24543E-01 which is output to the screen.. > I can also output it like this > > Put(Item=>Float 'Truncation(120.00*myFunction),Exp=>0,Aft=>5); > > to output 81.00000 to the screen. > > > Now I would like to save this 81.00000 as an integer to some variable. Can you please show me how to do this please > > > > declare An_Integer : Integer := Integer(Float'Truncation(120.00*myFunction)); begin Text_io.Put_Line(Integer'Image(An_Integer)); end; -- Björn ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-03 3:35 ` Stribor40 2014-10-03 7:45 ` Björn Lundin @ 2014-10-03 8:29 ` Jacob Sparre Andersen 2014-10-06 23:36 ` brbarkstrom 1 sibling, 1 reply; 19+ messages in thread From: Jacob Sparre Andersen @ 2014-10-03 8:29 UTC (permalink / raw) Stribor40 wrote: > Now I would like to save this 81.00000 as an integer to some > variable. Can you please show me how to do this please You can't do that without having declared (or chosen) an appropriate integer type, and declared a variable of that type. Once that is done, you write: Variable := Type (81.00000); Greetings, Jacob -- "constructive ambiguity" ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-03 8:29 ` Jacob Sparre Andersen @ 2014-10-06 23:36 ` brbarkstrom 2014-10-07 0:03 ` Jeffrey Carter 0 siblings, 1 reply; 19+ messages in thread From: brbarkstrom @ 2014-10-06 23:36 UTC (permalink / raw) On Friday, October 3, 2014 4:29:34 AM UTC-4, Jacob Sparre Andersen wrote: > Stribor40 wrote: > > > > > Now I would like to save this 81.00000 as an integer to some > > > variable. Can you please show me how to do this please > > > > You can't do that without having declared (or chosen) an appropriate > > integer type, and declared a variable of that type. > > > > Once that is done, you write: > > > > Variable := Type (81.00000); > > > > Greetings, > > > > Jacob > > -- > > "constructive ambiguity" First, to be safe in doing any floating point arithmetic, you should use the Ada type long_float. You can rename this to double or real if you want. Create a package that that creates a new Ada.Numerics (or some such - I need to check the definitions I usually use on my Windows XP machine that doesn't connect to the Web) Next, the equivalent to type casting is that you can then create a function like With Common_Defs; use Common_Defs; function Multiply(x : in float; y : in float) return real; function Multiply(x : in float; y : in float) return real is Result : real; begin Result := real(x*y); return (Result); end Multiply; You will reduce both truncation and round-off errors by doing all floating point computations with Ada long_float variables and operations. If you do, just replace the "float" types in the snippet with "real" and carry on. If you need an introduction, consult Acton, 1970: "Numerical Methods That Work", Haroer & Row. It's the only book I know on numerical methods that has a sense of humor - as well as a whole interlude on "What Not To Compute". Bruce B. ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-06 23:36 ` brbarkstrom @ 2014-10-07 0:03 ` Jeffrey Carter 2014-10-07 0:21 ` brbarkstrom 0 siblings, 1 reply; 19+ messages in thread From: Jeffrey Carter @ 2014-10-07 0:03 UTC (permalink / raw) On 10/06/2014 04:36 PM, brbarkstrom@gmail.com wrote: > > First, to be safe in doing any floating point arithmetic, you should > use the Ada type long_float. No, you shouldn't. First, Long_Float is optional; there is no guarantee that a compiler implements it, so if you want your code to be portable you'll avoid it. Even if it is defined, it need not be any different from Float. Second, given that McCormick identified modeling scalars (using user-defined numeric and enumeration types) as the main Ada feature that allowed his students to be successful, advice to always use a predefined type should rightly be regarded with suspicion. If you have an application that needs the maximum precision the compiler can give you, you should define a floating-point type using System.Max_Digits. > return (Result); There is no need for parentheses here. -- Jeff Carter "I was hobbling along, minding my own business, all of a sudden, up he comes, cures me! One minute I'm a leper with a trade, next minute my livelihood's gone! Not so much as a 'by your leave!' You're cured, mate. Bloody do-gooder!" Monty Python's Life of Brian 76 ^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: casting types 2014-10-07 0:03 ` Jeffrey Carter @ 2014-10-07 0:21 ` brbarkstrom 0 siblings, 0 replies; 19+ messages in thread From: brbarkstrom @ 2014-10-07 0:21 UTC (permalink / raw) On Monday, October 6, 2014 8:03:44 PM UTC-4, Jeffrey Carter wrote: > On 10/06/2014 04:36 PM, brbarkstrom@gmail.com wrote: > > > > > > First, to be safe in doing any floating point arithmetic, you should > > > use the Ada type long_float. > > > > No, you shouldn't. First, Long_Float is optional; there is no guarantee that a > > compiler implements it, so if you want your code to be portable you'll avoid it. > > Even if it is defined, it need not be any different from Float. Second, given > > that McCormick identified modeling scalars (using user-defined numeric and > > enumeration types) as the main Ada feature that allowed his students to be > > successful, advice to always use a predefined type should rightly be regarded > > with suspicion. > > > > If you have an application that needs the maximum precision the compiler can > > give you, you should define a floating-point type using System.Max_Digits. > > > > > return (Result); > > > > There is no need for parentheses here. > > > > -- > > Jeff Carter > > "I was hobbling along, minding my own business, all of a > > sudden, up he comes, cures me! One minute I'm a leper with > > a trade, next minute my livelihood's gone! Not so much as a > > 'by your leave!' You're cured, mate. Bloody do-gooder!" > > Monty Python's Life of Brian > > 76 Thanks for the correction. ^ permalink raw reply [flat|nested] 19+ messages in thread
end of thread, other threads:[~2014-10-07 0:21 UTC | newest] Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2014-10-01 1:49 casting types Stribor40 2014-10-01 2:06 ` Jeffrey Carter 2014-10-01 2:13 ` Stribor40 2014-10-01 2:56 ` Stribor40 2014-10-01 13:18 ` Dennis Lee Bieber 2014-10-01 13:55 ` Stribor40 2014-10-01 14:23 ` G.B. 2014-10-01 17:16 ` Stribor40 2014-10-01 17:38 ` Dirk Heinrichs 2014-10-02 1:47 ` Dennis Lee Bieber 2014-10-01 18:11 ` Jeffrey Carter 2014-10-02 9:21 ` Brian Drummond 2014-10-02 9:24 ` Brian Drummond 2014-10-03 3:35 ` Stribor40 2014-10-03 7:45 ` Björn Lundin 2014-10-03 8:29 ` Jacob Sparre Andersen 2014-10-06 23:36 ` brbarkstrom 2014-10-07 0:03 ` Jeffrey Carter 2014-10-07 0:21 ` brbarkstrom
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox