Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are multiples of 0.25 exactly representable as double?

I have the following code for finding quartiles:

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    double qrt[3];
    double *value;
    int count;
} t_data;

static void set_qrt(t_data *data, int qrt)
{
    int n, e;
    double d;

    d = qrt * 0.25 * data->count + 0.5;
    n = (int)d;
    e = n != d;
    data->qrt[qrt - 1] = data->value[n - 1];
    if (e) {
        data->qrt[qrt - 1] += data->value[n];
        data->qrt[qrt - 1] *= 0.5;
    }
}

static void set_qrts(t_data *data)
{
    set_qrt(data, 2);
    if (data->count > 1) {
        set_qrt(data, 1);
        set_qrt(data, 3);
    } else {
        data->qrt[0] = 0.0;
        data->qrt[2] = 0.0;
    }
}

static int comp(const void *pa, const void *pb)
{
    const double a = *(const double *)pa;
    const double b = *(const double *)pb;

    return (a > b) ? 1 : (a < b) ? -1 : 0;
}

int main(void)
{
    double values[] = {3.7, 8.9, 7.1, 5.4, 1.2, 6.8, 4.3, 2.7};
    t_data data;

    data.value = values;
    data.count = (int)(sizeof(values) / sizeof(double));
    qsort(data.value, data.count, sizeof(double), comp);
    set_qrts(&data);
    printf("Q1 = %.1f\nQ2 = %.1f\nQ3 = %.1f\n", data.qrt[0], data.qrt[1], data.qrt[2]);
}

Is

d = qrt * 0.25 * data->count + 0.5;
n = (int)d;
e = n != d;

guaranteed to work as expected? (e == isinteger(d))

like image 666
David Ranieri Avatar asked Jul 30 '13 10:07

David Ranieri


1 Answers

Numbers 0.5, 0.25, 0.125 and so on represent negative powers of two, and therefore are representable exactly in IEEE 754 types. Using these numbers does not result in representation errors.

like image 92
Sergey Kalinichenko Avatar answered Nov 15 '22 08:11

Sergey Kalinichenko