B
BigMan
How can I check if assignment of a float to a double (or vice versa)
will result in loss of precision?
will result in loss of precision?
BigMan said:How can I check if assignment of a float to a double (or vice versa)
will result in loss of precision?
Victor said:Assignment of a float to a double is always precise. Assignment of
a double to a float will cause loss of precision if the double has
non-zero bits in the part of its mantissa beyond the size of the
mantissa of the float. There is no platform-independent check that
you could do, it all depends on the representation of the float and
double types.
V
Bogdan said:double d = ...;
float f = (float)d;
bool loss = (d != (double)f);
?
Agree , but those are the implementation details of 'some' functionVictor said:I think this measures if the assignment _has_resulted_ in a loss of
precision, not if it _will_ result, don't you agree?
mantissa of the float. There is no platform-independent check that
you could do, it all depends on the representation of the float and
double types.
David said:On Tue, 12 Apr 2005 09:27:22 -0400 in comp.lang.c++, Victor Bazarov
What then is wrong with the following reasoning? If, after assigning
the double to a float, the float compares equal to the double, then no
precision was lost, otherwise it was.
Of course I would never suggest using float if you can afford double.
Nothing is wrong except that the OP wanted to check if assignment "will
result in loss", not if it "has resulted in loss".
BigMan said:1. Does the C++ standard say that assigning a float to a double never
results in loss of precision? If so, where does it say so?
2. Where could I find more info about reducing the error of operations
on FP numbers?
No. How could it.
Assume: sizeof( double ) == 8, sizeof( float ) == 4
How can one pack 8 bytes into 4 without loosing anything?
ulrich said:imho, BigMan meant it the other way round: assign a float to a double...
BigMan said:1. Does the C++ standard say that assigning a float to a double never
results in loss of precision? If so, where does it say so?
2. Where could I find more info about reducing the error of operations
on FP numbers?
It does not.
Really?
Actually, it says the opposite: "An rvalue of type float
can be converted to an rvalue of type double. The value is unchanged."
(4.6/1)
Clark said:Isn't that exactly what BigMan said? If the value is "unchanged" how can
it be less precise? Wouldn't a loss of precision count as changing the
value?
What do you want from me? So, I didn't read the question very carefully.
He got his answer. The Standard does say the assignment never results in
loss of precision, and I quoted it and gave him the paragraph number.
BigMan said:How can I check if assignment of a float to a double (or vice versa)
will result in loss of precision?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.