comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Type casting
Date: Mon, 26 Oct 2009 13:09:12 -0700 (PDT)
Date: 2009-10-26T13:09:12-07:00	[thread overview]
Message-ID: <b843304f-2242-46b1-a152-425a7149d946@2g2000prl.googlegroups.com> (raw)
In-Reply-To: 1256585214.3272.12.camel@HERMES

On Oct 26, 12:26 pm, Bruno <brunomend...@gmail.com> wrote:
> Hi,
>
> I have a small mess with the conversion of numeric types in the
> following program
>
> http://paste.ideaslabs.com/show/q6vOGqlyYJ
>
> I really do not know how to interpret the operation (E / 2) because
> although the operation is composed of two numbers such as "Positive"
> outcome can be of type "Float" and, of course, the "Integer" (attribute)
> returns an integer but the closest to real number and what
> need my program is that it returns the integer part regardless
> the decimal part.

No, the outcome of E / 2 cannot be of type float.  In some other
programming languages it can.  But in Ada, if you divide two integers,
the result is an integer and is always truncated downward---the
remainder is thrown away.  In this example, the type conversion
Integer (E/2) is redundant since the result of E/2 is already an
integer.

If you say Integer (Float (E) / 2.0) then the result of the division
would be a float, and it would round *upward* when converting to an
integer (if E is odd).  But that isn't what you want for this
algorithm.

>
> I tried creating a small building under the following function
>
> http://paste.ideaslabs.com/show/7y97bn4G6
>
> but by requiring Float type parameters for the algorithm does not work
> prev.

I'm not sure what you mean.  It looks like this second example was
trying to truncate a float to an integer (toward zero), and it appears
that it works fine.  But you can do the same thing much more easily
with

   return Integer (Float'Truncation (F));

Float'Truncation truncates F to an integer, truncating toward zero,
but it leaves the result as a Float; when you convert that to an
integer, no truncation or rounding is necessary since the result of
Float'Truncation already has an integral value.

Anyway, I'm not sure what about this doesn't work.

Hope this helps,

                                 -- Adam




  parent reply	other threads:[~2009-10-26 20:09 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-07 12:40 Ada.Directories.Base_Name and dot files 
2009-10-07 14:46 ` Yannick Duchêne Hibou57
2009-10-07 17:06   ` 
2009-10-07 14:52 ` Adam Beneschan
2009-10-07 17:06   ` 
2009-10-07 15:10 ` Adam Beneschan
2009-10-07 15:37   ` Yannick Duchêne Hibou57
2009-10-11  2:39     ` Randy Brukardt
2009-10-07 20:11   ` 
2009-10-08  9:39 ` Stephen Leake
2009-10-08  9:58   ` 
2009-10-26 19:26     ` Type casting Bruno
2009-10-26 20:02       ` Jeffrey R. Carter
2009-10-26 20:09       ` Adam Beneschan [this message]
2009-10-26 19:27     ` Conversions Bruno
2009-10-26 20:09       ` Conversions Jeffrey R. Carter
2009-10-26 20:19       ` Conversions Adam Beneschan
replies disabled

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