From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.42.39.210 with SMTP id i18mr15957462ice.19.1399301659870; Mon, 05 May 2014 07:54:19 -0700 (PDT) X-Received: by 10.182.104.131 with SMTP id ge3mr6202obb.39.1399301659698; Mon, 05 May 2014 07:54:19 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.bbs-scene.org!xmission!news.glorb.com!c1no1673721igq.0!news-out.google.com!gi6ni747igc.0!nntp.google.com!r10no864842igi.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Mon, 5 May 2014 07:54:19 -0700 (PDT) In-Reply-To: <9ac10bab-2ade-4ac8-a99b-336595e38f67@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=66.126.103.122; posting-account=KSa2aQoAAACOxnC0usBJYX8NE3x3a1Xq NNTP-Posting-Host: 66.126.103.122 References: <9ac10bab-2ade-4ac8-a99b-336595e38f67@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <795f30aa-7cbb-4987-9ab2-9fc7656efc9c@googlegroups.com> Subject: Re: Noob question.. Autopromotion? From: Adam Beneschan Injection-Date: Mon, 05 May 2014 14:54:19 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: news.eternal-september.org comp.lang.ada:19688 Date: 2014-05-05T07:54:19-07:00 List-Id: On Monday, May 5, 2014 7:22:43 AM UTC-7, sdale...@gmail.com wrote: > Hello, > I've been reading this... >=20 > www.adacore.com/uploads_gems/Ada_for_the_C++_or_Java_Developer-cc.pdf >=20 > On page 14 it says... >=20 > "In Ada, a floating point literal must be written with both an integral a= nd decimal part. 10 is not a valid literal for a floating point value, whi= le 10.0 is." >=20 > In my use case I will have users entering values (likely to be whole inte= gers) and these will be multiplied by the price. >=20 > For example... >=20 > Buy 100 USD @ 1.1 CAD >=20 > However there is also the market order where the person will be buying or= selling at best available price and then we have to report the average pri= ce paid. >=20 > 110 CAD / 100 USD >=20 > So the question is, if I use some sort of floating point type for all 3 v= alues, what's going to happen during assignment? I can't necessarily check= user input ahead of time and force them to input 100.00, so is there a way= to autopromote the values? Thanks! What you're talking about here is not "auto-promotion". *Promotion* is a c= oncept that really applies only to code in your program. Variables and oth= er objects have types; some of the possible types are "integer" and "float"= ("int" and "float" in Java/C++). Numeric literals also have types. So if= the literal 100 appears in your program, it has a type; this is "universal= integer" in Ada, and "int" in Java or C++. Promotion describes what happe= ns when you use an integer in a place where a float is required. In Java a= nd C++, the "int" is automatically converted to a "float" ("double", whatev= er); in Ada, it's not automatically converted. So you have to write 100.0 = in your program to get a literal with a "universal real" type; or if you ha= ve an integer object, you have to write a type conversion. But all this applies only to literals that appear in your program. Data in= user input doesn't have types. It's just text. So talking about "promoti= on" is really the wrong concept here. All you want to know is, can you par= se a "100" and have it produce a float. And the answer here is Yes; the rules for Ada's built-in parsing of strings= are more lenient than the rules for how it interprets the literals in your= program. Both of these will work and will give you a floating-point value= of 100.0 if you give it "100" for the string S: Ada.Float_Text_IO.Get (S, Result_Float, Next_Index); Result_Float :=3D Float'Value(S); Of course, if there's anything about Ada's rules for parsing input that doe= sn't work for you, you can write your own code to do the parsing. -- Adam