Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #91408

Re: How can I get an implementation of fmod()?

From James Kuyper <jameskuyper@alumni.caltech.edu>
Newsgroups comp.lang.c++
Subject Re: How can I get an implementation of fmod()?
Date 2023-08-25 04:34 -0400
Organization A noiseless patient Spider
Message-ID <uc9p27$3ului$1@dont-email.me> (permalink)
References <b1f2568f-0221-4b79-b6c6-115abd875541n@googlegroups.com>

Show all headers | View raw


On 8/25/23 02:25, Megumin wrote:
> How does remainder() in cmath works ...

"The remainder functions compute the remainder x REM y required by IEC
60559. 259)" (7.12.10.2p2). Note that ISO/IEC 60559 is equivalent to
IEEE 754. That "259" refers to footnote 259, which quote that standard
as saying:

"When y ΜΈ= 0, the remainder r = x REM y is defined regardless of the
rounding mode by the mathematical relation r = x βˆ’ ny, where n is the
integer nearest the exact value of x/y; whenever |n βˆ’ x| = 1/2 , then n
is even. If r = 0, its sign shall be that of x."

"The roundeven functions round their argument to the nearest integer
value in floating-point format, rounding halfway cases to even (that is,
to the nearest value that is an even integer), regardless of the current
rounding direction." (7.12.9.8p2)

Therefore, an implementation could define

float remainderf(float x, thus y) {
return x - roundevenf(x/y)*y;
}

> and is there any simple but fairly precise fmod that can be
> implemented without using double or long double?

"The fmod functions return the value x βˆ’ ny, for some integer n such
that, if y is nonzero, the result has the same sign as x and magnitude
less than the magnitude of y. If y is zero, whether a domain error
occurs or the fmod functions return zero is implementation-defined."
(7.12.10.1p3).

"The trunc functions round their argument to the integer value, in
floating format, nearest to but no larger in magnitude than the
argument." (7.12.9.1p2)

Therefore, an implementation could define

float fmodf(float x,float y) {
return x - truncf(x/y)*y;
}

The "but no larger in magnitude" specification for trunc() guarantees
that this subtraction has the same sign as x, as is required for fmod().

> I'm currently trying to implement fmodf(float x,float y) myself in
> assembly, and seems like in IEEE754, if I try to do as follows, the
> precision will f-up badly if x>>y in fact if x/y is more than 2^23
> this will just makes no sense at all.

You say that you're doing it in assembly, which I couldn't help you
with, but the following looks a lot more like C than assembly.

> r1 = f32(x);
> r2 = f32(y);
> r3 = div_f32(r1,r2); //precision:2ulp
> r4 = trunc(r3)// this is where things go bad if r3 is more than 2^23
> it does nothing and I will get no remainder.
> r4 = multi_f32(r4,r2); r4 = subtract_f32(r1,r4);
> result = r4;
I suspect that it is not possible, and therefore not required by IEC
754, to produce a more accurate result than that in those cases. On the
other hand, it's 03:42 here right now, and I couldn't find my copy of
IEEE 754, so I can't be sure that I'm remembering and thinking about
that correctly.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How can I get an implementation of fmod()? Megumin <chrisramirez0227@gmail.com> - 2023-08-24 23:25 -0700
  Re: How can I get an implementation of fmod()? Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-25 09:19 +0200
    Re: How can I get an implementation of fmod()? Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-25 09:35 +0200
  Re: How can I get an implementation of fmod()? Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-25 09:49 +0200
  Re: How can I get an implementation of fmod()? James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-08-25 04:34 -0400
    Re: How can I get an implementation of fmod()? Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-25 10:37 +0200
      Re: How can I get an implementation of fmod()? Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-25 10:43 +0200
        Re: How can I get an implementation of fmod()? Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-25 11:20 +0200
          Re: How can I get an implementation of fmod()? Megumin <chrisramirez0227@gmail.com> - 2023-08-30 22:52 -0700
            Re: How can I get an implementation of fmod()? Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-31 11:00 +0200
            Re: How can I get an implementation of fmod()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-08-31 21:48 +0100
            Re: How can I get an implementation of fmod()? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-08-31 14:37 -0700
    Re: How can I get an implementation of fmod()? James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-08-25 04:44 -0400

csiph-web