From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,8fce73be1b24ff29 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news.glorb.com!newsfeed00.sul.t-online.de!t-online.de!grolier!usenet-fr.net!proxad.net!proxad.net!newsfeed.arcor.de!news.arcor.de!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: C fmod function Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.14.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <1n90xfycs8ygs$.a2i8smdqp0t0$.dlg@40tude.net> Date: Wed, 12 Apr 2006 21:23:51 +0200 Message-ID: <1fi71kv8vgdac.2y00dhf24nw3.dlg@40tude.net> NNTP-Posting-Date: 12 Apr 2006 21:23:35 MEST NNTP-Posting-Host: 296920e2.newsread2.arcor-online.net X-Trace: DXC=9B0;OBH\jfkCUhGd:LVK2eQ5U85hF6f;djW\KbG]kaMhAV6U:Z=fE=oEEJBeO[[5Am[6LHn;2LCVn[ 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