You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The implementation below only ensures correctness (not considering inf and nan), can be used to verify more efficient solutions (if there are any) in the future.
the integer = the floating point * 2^-(std::numeric_limits<T>::min_exponent-1) * 2^(std::numeric_limits<T>::digits - 1) (so the decimal point is moved to the right so it can be an integer)
actual operations used:
(similar to frexp) temp = the floating point * 2^(an integer necessary to make temp falls in [1, 2)) (exponent is changed);
temp = temp * 2^(std::numeric_limits<T>::digits - 1), temp is guaranteed to be an integer now, convert it to an integer (mantissa's decimal point moved to the right);
temp = temp * 2^-(std::numeric_limits<T>::min_exponent - 1) (exponent is changed);
temp = temp / 2^(the integer in step1) (exponent is changed) (now exponent (= original exponent - (std::numeric_limits<T>::min_exponent-1) + (std::numeric_limits<T>::digits - 1)) is guaranteed to be non-negative);
I do not think that you need to convert floats to ints in order to have exact arithmetic. Also in the floating point format, all arithmetic operations are exact if do not exhaust the bits in the mantissa (equivalent to integer overflow) and if you only divide by numbers that do not leave a residual.
@HDembinski The problem occurs when if do not exhaust the bits in the mantissa is not satisfied. In the example above, 2e+8 - 1 and 4e+8 - 1 can not be stored in float exactly (the least significant bits are lost).
because x -= std::floor((4e+8 - 1) / (2e+8 - 1)) * (2e+8 - 1); is actually x -= std::floor(4e+8 / 2e+8) * 2e+8; make x become 0, 0's index in { 1., 1e+8, 2e+8, } is -1 (should be impossible to be returned by index).
Activity
jhcarl0814 commentedon Dec 27, 2022
The implementation below only ensures correctness (not considering inf and nan), can be used to verify more efficient solutions (if there are any) in the future.
turn a floating point into an integer of
/* the leading 1 is implicit */ 1 + matissa + (std::numeric_limits<T>::max_exponent - std::numeric_limits<T>::min_exponent)=1 + (std::numeric_limits<T>::digits - 1) + (std::numeric_limits<T>::max_exponent - std::numeric_limits<T>::min_exponent)bits (--std=gnu++14 -O2 -Wall -pedantic -pthread -lquadmath) (-std=gnu++14and-lquadmathare only forboost::multiprecision::float128demonstration).the integer = the floating point * 2^
-(std::numeric_limits<T>::min_exponent-1)* 2^(std::numeric_limits<T>::digits - 1)(so the decimal point is moved to the right so it can be an integer)actual operations used:
frexp) temp = the floating point * 2^(an integer necessary to make temp falls in[1, 2)) (exponent is changed);(std::numeric_limits<T>::digits - 1), temp is guaranteed to be an integer now, convert it to an integer (mantissa's decimal point moved to the right);-(std::numeric_limits<T>::min_exponent - 1)(exponent is changed);= original exponent - (std::numeric_limits<T>::min_exponent-1) + (std::numeric_limits<T>::digits - 1)) is guaranteed to be non-negative);turn a floating point value into an integer, and then use integer arithmetic to get correct results.
HDembinski commentedon Jan 11, 2023
I do not think that you need to convert floats to ints in order to have exact arithmetic. Also in the floating point format, all arithmetic operations are exact if do not exhaust the bits in the mantissa (equivalent to integer overflow) and if you only divide by numbers that do not leave a residual.
jhcarl0814 commentedon Jan 12, 2023
@HDembinski The problem occurs when
if do not exhaust the bits in the mantissais not satisfied. In the example above,2e+8 - 1and4e+8 - 1can not be stored infloatexactly (the least significant bits are lost).because
x -= std::floor((4e+8 - 1) / (2e+8 - 1)) * (2e+8 - 1);is actuallyx -= std::floor(4e+8 / 2e+8) * 2e+8;makexbecome0,0's index in{ 1., 1e+8, 2e+8, }is-1(should be impossible to be returned byindex).