comp.lang.ada
 help / color / mirror / Atom feed
* Noob question..  Autopromotion?
@ 2014-05-05 14:22 sdalemorrey
  2014-05-05 14:54 ` Adam Beneschan
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: sdalemorrey @ 2014-05-05 14:22 UTC (permalink / raw)


Hello,

I've been reading this...

www.adacore.com/uploads_gems/Ada_for_the_C++_or_Java_Developer-cc.pdf

On page 14 it says...

"In Ada, a floating point literal must be written with both an integral and decimal part.  10 is not a valid literal for a floating point value, while 10.0 is."

In my use case I will have users entering values (likely to be whole integers) and these will be multiplied by the price.

For example...

Buy 100 USD @ 1.1 CAD

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 price paid.

110 CAD / 100 USD

So the question is, if I use some sort of floating point type for all 3 values, 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!


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Noob question..  Autopromotion?
  2014-05-05 14:22 Noob question.. Autopromotion? sdalemorrey
@ 2014-05-05 14:54 ` Adam Beneschan
  2014-05-05 15:32 ` Nasser M. Abbasi
  2014-05-06 13:31 ` gautier_niouzes
  2 siblings, 0 replies; 6+ messages in thread
From: Adam Beneschan @ 2014-05-05 14:54 UTC (permalink / raw)


On Monday, May 5, 2014 7:22:43 AM UTC-7, sdale...@gmail.com wrote:
> Hello,
> I've been reading this...
> 
> www.adacore.com/uploads_gems/Ada_for_the_C++_or_Java_Developer-cc.pdf
> 
> On page 14 it says...
> 
> "In Ada, a floating point literal must be written with both an integral and decimal part.  10 is not a valid literal for a floating point value, while 10.0 is."
> 
> In my use case I will have users entering values (likely to be whole integers) and these will be multiplied by the price.
> 
> For example...
> 
> Buy 100 USD @ 1.1 CAD
> 
> 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 price paid.
> 
> 110 CAD / 100 USD
> 
> So the question is, if I use some sort of floating point type for all 3 values, 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 concept that really applies only to code in your program.  Variables and other 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 happens when you use an integer in a place where a float is required.  In Java and C++, the "int" is automatically converted to a "float" ("double", whatever); 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 have 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 "promotion" is really the wrong concept here.  All you want to know is, can you parse 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 := Float'Value(S);

Of course, if there's anything about Ada's rules for parsing input that doesn't work for you, you can write your own code to do the parsing.

                               -- Adam

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Noob question..  Autopromotion?
  2014-05-05 14:22 Noob question.. Autopromotion? sdalemorrey
  2014-05-05 14:54 ` Adam Beneschan
@ 2014-05-05 15:32 ` Nasser M. Abbasi
  2014-05-05 19:30   ` Simon Wright
  2014-05-06 13:31 ` gautier_niouzes
  2 siblings, 1 reply; 6+ messages in thread
From: Nasser M. Abbasi @ 2014-05-05 15:32 UTC (permalink / raw)


On 5/5/2014 9:22 AM, sdalemorrey@gmail.com wrote:

>
> So the question is, if I use some sort of floating point type for all 3 values,
>

Would not using Fixed point type be better for financial application?

https://www2.adacore.com/gap-static/GNAT_Book/html/aarm/AA-3-5-9.html

Ada is one of few languages that supports fixed point type
as build-in data type.

--Nasser

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Noob question..  Autopromotion?
  2014-05-05 15:32 ` Nasser M. Abbasi
@ 2014-05-05 19:30   ` Simon Wright
  0 siblings, 0 replies; 6+ messages in thread
From: Simon Wright @ 2014-05-05 19:30 UTC (permalink / raw)


"Nasser M. Abbasi" <nma@12000.org> writes:

> On 5/5/2014 9:22 AM, sdalemorrey@gmail.com wrote:
>
>>
>> So the question is, if I use some sort of floating point type for
>> all 3 values,
>>
>
> Would not using Fixed point type be better for financial application?
>
> https://www2.adacore.com/gap-static/GNAT_Book/html/aarm/AA-3-5-9.html
>
> Ada is one of few languages that supports fixed point type
> as build-in data type.
>
> --Nasser

I'd have thought you'd use Annex F's decimal arithmetic[1], if your
compiler supports it.

It might be better to link to a more up-to-date version of the ARM! [2]

[1] https://www2.adacore.com/gap-static/GNAT_Book/html/aarm/AA-F-2.html
[2] http://www.ada-auth.org/standards/12rm/html/RM-TTL.html


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Noob question..  Autopromotion?
  2014-05-05 14:22 Noob question.. Autopromotion? sdalemorrey
  2014-05-05 14:54 ` Adam Beneschan
  2014-05-05 15:32 ` Nasser M. Abbasi
@ 2014-05-06 13:31 ` gautier_niouzes
  2014-05-07 16:13   ` jpwoodruff
  2 siblings, 1 reply; 6+ messages in thread
From: gautier_niouzes @ 2014-05-06 13:31 UTC (permalink / raw)


Hello,
As Adam said, a figure read as a literal in the Ada source code and a figure read from a string from an user are different topics. Typically figures with or without .0 are valid (to GNAT and OA run-time libraries at least).

   with ada.text_io;
   procedure value is
   begin
     ada.text_io.put_line(float'image(float'Value("1234.0")));
     ada.text_io.put_line(float'image(float'Value("1234")));
   end;

Now, you may have user inputs with thousands separators and/or percents, for instance - especially if the input is actually a slight modification of an output). So you will certainly need a custom solution anyway.
Here is a function dealing with both (filtering thousands separators, and doing *0.01 with percent signs):

  function Value(s: String) return Real is
    t: constant String:= Trim(s, Both);
    ft: String(t'Range);
    j: Integer;
  begin
    if t'Length > 0 and then t(t'Last) = '%' then
      return Value(t(t'First .. t'Last-1)) * 0.01;
    elsif Index(t, (1 => thousands_separator)) = 0 then
      return Real'Value(t);
    else -- filter all thousands separators...
      j:= ft'First - 1;
      for i in t'Range loop
        if t(i) /= thousands_separator then
          j:= j + 1;
          ft(j):= t(i);
        end if;
      end loop;
      return Value(ft(ft'First .. j));
    end if;
  end Value;
_________________________
Gautier's Ada programming
http://gautiersblog.blogspot.com/search/label/Ada
NB: follow the above link for a valid e-mail address 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Noob question..  Autopromotion?
  2014-05-06 13:31 ` gautier_niouzes
@ 2014-05-07 16:13   ` jpwoodruff
  0 siblings, 0 replies; 6+ messages in thread
From: jpwoodruff @ 2014-05-07 16:13 UTC (permalink / raw)


On Tuesday, May 6, 2014 7:31:13 AM UTC-6, gautier...@hotmail.com wrote:
> Hello,
> 
> As Adam said, a figure read as a literal in the Ada source code and a figure read from a string from an user are different topics. Typically figures with or without .0 are valid (to GNAT and OA run-time libraries at least).
> 
Perhaps you might benefit from some old (1987 vintage) work of mine
that is useful for input of numeric (floating) values.

"The packages Numeric_IO and Name_IO, together with their children and
support, assist a program to read a user's input.  The packages are
intended to support numerical computation by providing Get and Put
procedures for floating numbers and for vectors and matrices of
floating numbers.

"The procedures ease an end-user's burden in preparing inputs for
computational programs.  The rules for input of floating numbers are
relaxed so that program inputs need not conform to the strict Ada
syntax for floating numbers. Facilities allow input either from files
or interactively. Consistent policies throughout all the services
allow programs to address input errors, to prompt the end-user
interactively or to specify optional default values."

http://www.dmitry-kazakov.de/ada/Numeric-Name-IO.htm

John

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-05-07 16:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-05 14:22 Noob question.. Autopromotion? sdalemorrey
2014-05-05 14:54 ` Adam Beneschan
2014-05-05 15:32 ` Nasser M. Abbasi
2014-05-05 19:30   ` Simon Wright
2014-05-06 13:31 ` gautier_niouzes
2014-05-07 16:13   ` jpwoodruff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox