* Re: C fmod function
2006-04-12 15:49 ` Candida Ferreira
@ 2006-04-12 19:23 ` Dmitry A. Kazakov
2006-04-12 19:38 ` Candida Ferreira
2006-04-13 9:36 ` Candida Ferreira
2 siblings, 0 replies; 6+ messages in thread
From: Dmitry A. Kazakov @ 2006-04-12 19:23 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: C fmod function
2006-04-12 15:49 ` Candida Ferreira
2006-04-12 19:23 ` Dmitry A. Kazakov
@ 2006-04-12 19:38 ` Candida Ferreira
2006-04-13 9:36 ` Candida Ferreira
2 siblings, 0 replies; 6+ messages in thread
From: Candida Ferreira @ 2006-04-12 19:38 UTC (permalink / raw)
I was finally able to test it and Ada's Remainder is not the same as C++
fmod. C++ fmod can be expressed by the generic form fMod(x,y) = ((x / y) -
(Sign(x / y) * Floor(Abs(x / y)))) * y.
Candida
"Candida Ferreira" <cferreira@seehomepage.com> wrote in message
news:kk9%f.233449$zk4.160702@fe3.news.blueyonder.co.uk...
> 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.
>
> Best wishes,
> Candida
>
> ---
> Candida Ferreira, Ph.D.
> Chief Scientist, Gepsoft
> http://www.gene-expression-programming.com/author.asp
>
> GEP: Mathematical Modeling by an Artificial Intelligence
> (Springer Verlag edition 2006)
> http://www.gene-expression-programming.com/Books/index.asp
> Online Version:
> http://www.gene-expression-programming.com/GepBook/Introduction.htm
>
> Modeling Software:
> http://www.gepsoft.com/
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: C fmod function
2006-04-12 15:49 ` Candida Ferreira
2006-04-12 19:23 ` Dmitry A. Kazakov
2006-04-12 19:38 ` Candida Ferreira
@ 2006-04-13 9:36 ` Candida Ferreira
2 siblings, 0 replies; 6+ messages in thread
From: Candida Ferreira @ 2006-04-13 9:36 UTC (permalink / raw)
I was finally able to test it and Ada's Remainder is not the same as C++
fmod. C++ fmod can be expressed by the generic form fMod(x,y) = ((x / y) -
(Sign(x / y) * Floor(Abs(x / y)))) * y.
Candida
"Candida Ferreira" <cferreira@seehomepage.com> wrote in message
news:kk9%f.233449$zk4.160702@fe3.news.blueyonder.co.uk...
> 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.
>
> Best wishes,
> Candida
>
> ---
> Candida Ferreira, Ph.D.
> Chief Scientist, Gepsoft
> http://www.gene-expression-programming.com/author.asp
>
> GEP: Mathematical Modeling by an Artificial Intelligence
> (Springer Verlag edition 2006)
> http://www.gene-expression-programming.com/Books/index.asp
> Online Version:
> http://www.gene-expression-programming.com/GepBook/Introduction.htm
>
> Modeling Software:
> http://www.gepsoft.com/
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread