Splitting a floating point number into a sum of two other numbers, without rounding errors

85 views Asked by At

I don't know if it is possible to do this, but i need to split a floating point number in sum of two number...

For example assuming x is a floating point number and we want to split this in x = I + f, where I is the signed integer part while f is the signed fractional part, both floating number i guess such split could be implemented exactly (i.e. without fp error i mean).

I was wondering if it is possible to split somehow x = (I-1.0) + (f + 1.0), namely without floating point rounding error.

I've implemented on my own the split x = I + f then summing and adding 1.0 i have the second split i presented, but basically it's affected from floating point rounding error such operation.

1

There are 1 answers

0
Pascal Cuoq On BEST ANSWER

(using double-precision for the examples)

I was wondering if it is possible to split somehow x = (I-1.0) + (f + 1.0), namely without floating point rounding error.

There is no chance of obtaining such a split for all values. Take 0x1.0p-60, the integral part of which is 0.0 and the fractional part is 0x1.0p-60. f + 1.0 is inexact and produces 1.0, whereas I-1.0 is exact and produces -1.0.

It's not just that the addition in f + 1.0 is inexact: the floating-point value such that, when added -1.0, produces 0x1.0p-60, does not exist.

A floating-point value v can always be split exactly into the sum of fmod(v, 1.0) and v - fmod(v, 1.0), where the latter is an integer, and there is at least another way to split v exactly between an integer part and a fractional part using IEEE 754 “Floating-point remainder”, which is subtly distinct from fmod, but there is no way to split numbers close to 0 into the floating-point sum of 1.0 and another value.