HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Floating point rounding

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
floatingroundingpoint

Problem

Can an IEEE-754 floating point number = 0.0 and = N (where * and >= are appropriate IEEE-754 operators)

This comes from this question based on this documentation and the postgresql random function

Solution

Assuming round-to-nearest and that $N > 0$, then $N R 1$, and the nearest rounding is down. In both cases, $N R$ is less than $N$.

Upward rounding can cause a problem, not that it should ever be selected in the presence of unsuspecting users. Here's some C99 that prints "0\n1\n" on my machine.

#include 
#include 
#include 

int main(void) {
    double n = 10;
    double r = nextafter(1, 0);
    printf("%d\n", n == (n * r));
    fesetround(FE_UPWARD);
    printf("%d\n", n == (n * r));
}

Code Snippets

#include <fenv.h>
#include <math.h>
#include <stdio.h>

int main(void) {
    double n = 10;
    double r = nextafter(1, 0);
    printf("%d\n", n == (n * r));
    fesetround(FE_UPWARD);
    printf("%d\n", n == (n * r));
}

Context

StackExchange Computer Science Q#3185, answer score: 9

Revisions (0)

No revisions yet.