comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@acm.org
Subject: Re: Why no new posts ?
Date: Wed, 15 Nov 2000 07:24:12 GMT
Date: 2000-11-15T07:24:12+00:00	[thread overview]
Message-ID: <wSqQ5.423947$i5.7280099@news1.frmt1.sfba.home.com> (raw)
In-Reply-To: 002a01c04eca$dbaecc00$b0375140@Fudge

with Ada.Text_IO,
     Ada.Float_Text_IO;
procedure Test is

  type Float9 is digits 9;
  package Float9_IO is new Ada.Text_IO.Float_IO(Float9);

  Number_String: constant String := "12345678";
  Digit_Value: constant array (Character range '0' .. '9') of Float
    := (0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);

  S : String := "123";

--  For example, is there a simple way to convert a string '123' into an
--integer - is there a simple command for this in Ada?
  N : Integer := Integer'Value(S);

--  Similarly, is there a simple way to convert Integers into Floats?
  F : Float := Float(N);

--  I would also like to know if there is a standard function for generating
--floats.  For example, Float(mantissa, exponent) = mantissa * 10 ** exponent,
  Avogadro: Float := 6.02 * 10.0 ** 23;

  Number  : Float := 0.0;    -- implementation defined Float
  Number9 : Float9 := 0.0;   -- "digits 9" float

begin
  Ada.Text_IO.Put_Line("S=" & S);
  Ada.Text_IO.Put_Line("N=" & Integer'Image(N));
  Ada.Text_IO.Put_Line("F=" & Float'Image(F));
  Ada.Text_IO.Put_Line("Avogadro=" & Float'Image(Avogadro));

  for I in Number_String'range loop
    Number := Number + Digit_Value(Number_String(I))
                       * 10.0 ** (Number_String'Last - I);
  end loop;

  Ada.Text_IO.Put_Line("Float'digits=" & Integer'image(Float'digits));
  Ada.Text_IO.Put_Line("Number=" & Float'Image(Number));
  Ada.Text_IO.Put_Line("Number/100=" & Float'Image(Number / 100.0));
  Ada.Float_Text_IO.Put(Number / 100.0, Exp => 0);
  Ada.Text_IO.New_Line;

  for I in Number_String'range loop
    Number9 := Number9 + Float9(Digit_Value(Number_String(I))
                                * 10.0 ** (Number_String'Last - I));
  end loop;

  Ada.Text_IO.Put_Line("Float9'digits=" & Integer'image(Float9'digits));
  Ada.Text_IO.Put_Line("Number9=" & Float9'Image(Number9));
  Ada.Text_IO.Put_Line("Number9/100=" & Float9'Image(Number9 / 100.0));
  Float9_IO.Put(Number9);Ada.Text_IO.New_Line;
  Float9_IO.Put(Number9 / 100.0, Exp => 0);Ada.Text_IO.New_Line;
end Test;
-------------- prints, on Win95 using Gnat 3.13p:
S=123
N= 123
F= 1.23000E+02
Avogadro= 6.02000E+23
Float'digits= 6
Number= 1.23457E+07
Number/100= 1.23457E+05
123456.78125
Float9'digits= 9
Number9= 1.23456780E+07
Number9/100= 1.23456780E+05
 1.23456780E+07
123456.78000000



  parent reply	other threads:[~2000-11-15  7:24 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-14  0:00 Why no new posts ? William J. Thomas
2000-11-14  0:00 ` Vincent Marciante
2000-11-15  7:33   ` Chris Miller
     [not found] ` <002001c04ec8$27db8ee0$b0375140@Fudge>
2000-11-15  0:00   ` David Starner
2000-11-15  0:00   ` Why no new posts ? Float accuracy Nicolas Brunot
2000-11-15  0:00     ` Jean-Pierre Rosen
2000-11-15  0:00   ` Why no new posts ? Martin Dowie
     [not found] ` <002a01c04eca$dbaecc00$b0375140@Fudge>
2000-11-15  7:24   ` tmoran [this message]
2000-11-15  7:38   ` tmoran
2000-11-16  2:44   ` DuckE
replies disabled

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