comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: C fmod function
Date: Wed, 12 Apr 2006 21:23:51 +0200
Date: 2006-04-12T21:23:35+02:00	[thread overview]
Message-ID: <1fi71kv8vgdac.2y00dhf24nw3.dlg@40tude.net> (raw)
In-Reply-To: kk9%f.233449$zk4.160702@fe3.news.blueyonder.co.uk

On Wed, 12 Apr 2006 15:49:36 GMT, Candida Ferreira wrote:

> Dmitry A. Kazakov wrote:
>>> Is there in Ada a built-in function equivalent to the fmod function in 
>>> C++?
>>
>> Yes. See Annex A.5.3, Remainder attribute defined for floating-point 
>> types.
> 
> Thanks, Dmitry. I was aware of the Remainder attribute, but having read the 
> definition I wasn't sure if it corresponded exactly to the C++ fmod function 
> which can be represented by the generic expression fMod(x,y) = ((x / y) - 
> (Sign(x / y) * Floor(Abs(x / y)))) * y.

Yes, you are right. The above rounds towards zero and the result has the
sign of x. Ada's remainder rounds to the nearest number, so the sign can be
any and the result's absolute value is no greater than |y/2|.

You can convert it to fmod as follows:

f := S'Remainder (x, y);
if Sign (x) = Sign (f) then
   return f;
elsif Sign (y) = Sign (f) then
   return f - y;
else
   return f + y;
end if;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2006-04-12 19:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-04-12 14:10 C fmod function Candida Ferreira
2006-04-12 15:39 ` Dmitry A. Kazakov
2006-04-12 15:49   ` Candida Ferreira
2006-04-12 19:23     ` Dmitry A. Kazakov [this message]
2006-04-12 19:38     ` Candida Ferreira
2006-04-13  9:36     ` Candida Ferreira
replies disabled

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