Why do we need IEEE 754 remainder?
Asked Answered
M

2

8

I just read this topic (especially the last comments).

Then I was wondering, why we actually need this was of giving the remainder. But it seems, that not many people "on google" were interested in that before...

Mayday answered 31/10, 2014 at 10:4 Comment(1)
@MarkDickinson what does that mean? Can you add that as a complete answer?Tophole
T
8

If you're looking for reasons why you would want it, one is for what is known as "range reduction"

Let's say you want sind function for computing the sine of an argument in degrees. A naive way to do this would be

sind(x) = sin(x*pi/180) 

However pi here is not the true irrational number pi, but instead the floating point number closest to pi. This leads to things like sind(180) == 1.2246467991473532e-16, and SO questions like this and this (and many, many more).

But sine is a periodic function, so if we compute

remainder(x,90.0)

we get a value on the interval [-45,45]. Note that 0, 90, 180, 270, etc. become exactly 0, and multiplying by pi/180 is still 0. Therefore taking the appropriately signed sin or cos, we can get the exact result at these values (and if you do some basic error analysis, you can demonstrate that it also reduces the error at other values).

Two follow up points:

  1. How do you determine which sin or cos to use? Well, that's what remquo is for.
  2. Unfortunately, this still won't give sind(30.0) == 0.5 exactly, due to the vagaries of intermediate rounding. There are ways to fix this, e.g. see what the Julia library does.
Talebearer answered 9/12, 2014 at 11:40 Comment(4)
So to sum it up roughly: It's used in mathmatics for getting more correct results on irrational numbers, right? Thanks for the explanation. I figured I'm not deep enough into this to understand it completely. Still some things to learn.Mayday
It's a great example, which made it click for me - any periodic "float" quantity (like angles). I know dozens of applications of modulus (mostly on integers though), but despite seeing the symmetry in remainder I really couldn't guess what this powerful technology was for. So thanks for the question and answer.Sweatband
So a remainder of N produces a range of (-N/2, N/2)?Grouchy
@AaronFranke Yes, but the interval is closed [-N/2, N/2].Talebearer
L
0

Can be used to get the nearest multiple:

double NearestMultiple(int numerator, int denominator)
{
    return numerator - double.Ieee754Remainder(numerator, denominator);
}

> NearestMultiple(11, 3)
12
> NearestMultiple(10, 3)
9
Lithium answered 13/8 at 17:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.