Question
For the C99 compiler implements exact arithmetic IEEE 754, there are values f, divisorsuch as float, such that f / divisor != (float)(f * (1.0 / divisor))?
EDIT: "By doing the exact arithmetic of IEEE 754," I mean a compiler that rightly defines FLT_EVAL_METHOD as 0.
Context
The AC compiler, which provides IEEE 754 floating point compatibility, can only replace single-point division by constant by one multiplication by the opposite, if the specified inverter itself is represented exactly as float.
In practice, this happens only for the powers of the two. Therefore, programmer Alex can be sure that it f / 2.0fwill be compiled as if it were f * 0.5f, but if it is permissible for Alex to multiply by 0.10finstead of dividing by 10, Alex must express this by writing multiplication in the program or using a compiler parameter such as GCC -ffast-math.
This question is related to the conversion of division with one precision into multiplication with double precision. Does it always give the correct rounded result? Is it likely that this could be cheaper, and therefore be an optimization that compilers can do (even without -ffast-math)?
(float)(f * 0.10) f / 10.0f f 1 2, . float, .
:
#include <float.h>
#include <math.h>
#include <stdio.h>
int main(void){
for (float divisor = 1.0; divisor != 2.0; divisor = nextafterf(divisor, 2.0))
{
double factor = 1.0 / divisor;
for (float f = 1.0; f != 2.0; f = nextafterf(f, 2.0))
{
float cr = f / divisor;
float opt = f * factor;
if (cr != opt)
printf("For divisor=%a, f=%a, f/divisor=%a but (float)(f*factor)=%a\n",
divisor, f, cr, opt);
}
}
}
, (2 46). . - , - , , , , ?