comp.lang.ada
 help / color / mirror / Atom feed
From: brbarkstrom@gmail.com
Subject: Re: casting types
Date: Mon, 6 Oct 2014 16:36:05 -0700 (PDT)
Date: 2014-10-06T16:36:05-07:00	[thread overview]
Message-ID: <5e669b7a-21ac-46cf-b850-50692c38541d@googlegroups.com> (raw)
In-Reply-To: <87iok1d09g.fsf@adaheads.sparre-andersen.dk>

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.

  reply	other threads:[~2014-10-06 23:36 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2014-10-07  0:03             ` Jeffrey Carter
2014-10-07  0:21               ` brbarkstrom
replies disabled

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