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


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

Re: There is no "correct" result for std::fmod across all IEEE 754 representations

From Michael S <already5chosen@yahoo.com>
Newsgroups comp.lang.c++
Subject Re: There is no "correct" result for std::fmod across all IEEE 754 representations
Date 2025-03-02 11:07 +0200
Organization A noiseless patient Spider
Message-ID <20250302110710.00003fa2@yahoo.com> (permalink)
References <6in6sjtgdf82rmi09a5q21l7lp19vagb4m@4ax.com> <20250301230320.000074a4@yahoo.com> <tcv6sj1c4j6643nfcfk7jp937dspbksrdf@4ax.com>

Show all headers | View raw


On Sat, 01 Mar 2025 21:57:13 +0000
Mr Flibble <leigh@i42.co.uk> wrote:

> On Sat, 1 Mar 2025 23:03:20 +0200, Michael S
> <already5chosen@yahoo.com> wrote:
> 
> >On Sat, 01 Mar 2025 19:27:46 +0000
> >Mr Flibble <leigh@i42.co.uk> wrote:
> >  
> >> Hi!
> >> 
> >> There is no "correct" result for std::fmod across all IEEE 754
> >> representations.
> >> 
> >> Why?
> >> 
> >> #include <cmath>
> >> #include <iostream>
> >>  
> >> int main()
> >> {
> >>     std::cout << "Result of std::fmod(1.0e42, 1000.0): " <<
> >> std::fmod(1.0e42, 1000.0);
> >> }
> >> 
> >> Result of std::fmod(1.0e42, 1000.0): 312
> >> 
> >> The correct answer mathematically is 0 not 312 but as IEEE 754 has
> >> a fixed width mantissa, x is inprecise so there is no correct
> >> "precise" result for std::fmod(x, y) for all x & y as Bonita
> >> Montero claims rendering her test case utter bollocks.
> >> 
> >> Given these FACTS please present actual problems with:
> >> 
> >> double my_fmod(double x, double y) 
> >> {
> >>     if (y == 0.0)
> >> 	return x / y;
> >>     return x - std::trunc(x / y) * y;
> >> }
> >> 
> >> /Flibble  
> >
> >
> >For x that is a closest IEEE-754 binary64 approximation of 1e42, i.e.
> >for 0x1.6f578c4e0a061p+139, 312 is an exact answer.  
> 
> The 312 doesn't come directly from the mantissa though, if it did it
> would be 0 not 312. 

I don't quite understand what you mean by that. That it does not come
from *decimal* mantissa that you wrote in the source code?
But how could it? IEEE-754 binary64 is, well, binary.

Python:
>>> x=1e42
>>> x
1e+42
>>> int(x)
1000000000000000044885712678075916785549312


> My point is std::fmod is almost useless
> MATHEMATICALLY unless the arguments don't involve approximation, but
> with IEEE 754 floating point you get what you pay for.
> 
> /Flibble

If you were saying that fmod(x/y) for big values of x/y is almost
useless physically / engineerially then I'd tend to agree.
When physicist or engineer says 'double x= bar;' where bar is a number
that can be represented exactly in binary64 then what he means (if he
cared at all to think about what he means, which is rare) is that he
believes that x is in range (bar-ULP:bar+ULP] and he hopes that
probability of x being within range [bar-ULP/2:bar+ULP/2] is much
higher that 0.5. So, obviously, as long as y is abs(y)<ULP(x) for
physicist fmod(x/y) is meaningless.

But the question of what is meaningful mathematically is different. The
answer depends on branch of mathematics we are interested in. Mostly I
tend to agree with you that it should depend on whether the argument
involves approximation or not. However, I know that I can not predict
what mathematician that specializes, for example, in numeric theory
could find meaningful or just interesting.



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


Thread

There is no "correct" result for std::fmod across all IEEE 754 representations Mr Flibble <leigh@i42.co.uk> - 2025-03-01 19:27 +0000
  Re: There is no "correct" result for std::fmod across all IEEE 754 representations Michael S <already5chosen@yahoo.com> - 2025-03-01 23:03 +0200
    Re: There is no "correct" result for std::fmod across all IEEE 754 representations Mr Flibble <leigh@i42.co.uk> - 2025-03-01 21:57 +0000
      Re: There is no "correct" result for std::fmod across all IEEE 754 representations Michael S <already5chosen@yahoo.com> - 2025-03-02 11:07 +0200
        Re: There is no "correct" result for std::fmod across all IEEE 754 Muttley@DastardlyHQ.org - 2025-03-02 09:17 +0000
          Re: There is no "correct" result for std::fmod across all IEEE 754 Michael S <already5chosen@yahoo.com> - 2025-03-02 16:03 +0200
            Re: There is no "correct" result for std::fmod across all IEEE 754 Muttley@dastardlyhq.com - 2025-03-02 16:56 +0000
        Re: There is no "correct" result for std::fmod across all IEEE 754 representations Mr Flibble <leigh@i42.co.uk> - 2025-03-02 15:24 +0000
        Re: There is no "correct" result for std::fmod across all IEEE 754 representations James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-03-02 17:37 -0500
          Re: There is no "correct" result for std::fmod across all IEEE 754 representations Michael S <already5chosen@yahoo.com> - 2025-03-03 13:20 +0200
  Re: There is no "correct" result for std::fmod across all IEEE 754 representations Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-03 16:25 +0100
    Re: There is no "correct" result for std::fmod across all IEEE 754 representations Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-03 16:30 +0100
      Re: There is no "correct" result for std::fmod across all IEEE 754 representations Michael S <already5chosen@yahoo.com> - 2025-03-03 18:54 +0200
    Re: There is no "correct" result for std::fmod across all IEEE 754 representations Mr Flibble <leigh@i42.co.uk> - 2025-03-03 17:11 +0000
      Re: There is no "correct" result for std::fmod across all IEEE 754 representations Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-03 18:26 +0100
        Re: There is no "correct" result for std::fmod across all IEEE 754 representations Mr Flibble <leigh@i42.co.uk> - 2025-03-03 18:20 +0000
          Re: There is no "correct" result for std::fmod across all IEEE 754 representations Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-03 19:26 +0100
            Re: There is no "correct" result for std::fmod across all IEEE 754 representations Mr Flibble <leigh@i42.co.uk> - 2025-03-03 18:33 +0000
              Re: There is no "correct" result for std::fmod across all IEEE 754 representations Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-03 19:37 +0100
                Re: There is no "correct" result for std::fmod across all IEEE 754 representations Mr Flibble <leigh@i42.co.uk> - 2025-03-03 18:46 +0000
                Re: There is no "correct" result for std::fmod across all IEEE 754 representations Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-03 19:48 +0100
                Re: There is no "correct" result for std::fmod across all IEEE 754 representations Mr Flibble <leigh@i42.co.uk> - 2025-03-03 18:50 +0000
                Re: There is no "correct" result for std::fmod across all IEEE 754 representations Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-03 19:53 +0100
                Re: There is no "correct" result for std::fmod across all IEEE 754 representations Mr Flibble <leigh@i42.co.uk> - 2025-03-03 18:58 +0000
                Re: There is no "correct" result for std::fmod across all IEEE 754 representations Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-03 20:05 +0100
                Re: There is no "correct" result for std::fmod across all IEEE 754 representations Mr Flibble <leigh@i42.co.uk> - 2025-03-03 19:09 +0000
                Re: There is no "correct" result for std::fmod across all IEEE 754 representations Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-03 20:14 +0100
                Re: There is no "correct" result for std::fmod across all IEEE 754 representations Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-03 20:27 +0100
                Re: There is no "correct" result for std::fmod across all IEEE 754 representations Mr Flibble <leigh@i42.co.uk> - 2025-03-03 19:33 +0000
                Re: There is no "correct" result for std::fmod across all IEEE 754 representations Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-03 20:36 +0100
                Re: There is no "correct" result for std::fmod across all IEEE 754 representations Mr Flibble <leigh@i42.co.uk> - 2025-03-03 19:37 +0000
                Re: There is no "correct" result for std::fmod across all IEEE 754 representations Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-04 10:42 +0100
                Re: There is no "correct" result for std::fmod across all IEEE 754 representations Mr Flibble <leigh@i42.co.uk> - 2025-03-04 18:24 +0000
                Re: There is no "correct" result for std::fmod across all IEEE 754 representations "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2025-03-03 12:25 -0800

csiph-web