floating types

A

amit.codename13

from the c99 standard

6.3.1.4
When a value of integer type is converted to a real floating type,
if the value being
converted can be represented exactly in the new type, it is unchanged.
If the value being
converted is in the range of values that can be represented but cannot
be represented exactly, the result is either the nearest higher or
nearest lower representable value, chosen
in an implementation-defined manner. If the value being converted is
outside the range of
values that can be represented, the behavior is undefined.


since it says about a situation in which "value being converted can be
represented exactly in the new type" but does not say anything about
the requirements for it to be represented then it may mean that
sometimes the value can be represented and sometimes not???

why should anything like this be stated "if the value being converted
can be represented exactly in the new type"
 
E

Eric Sosman

from the c99 standard

6.3.1.4
When a value of integer type is converted to a real floating type,
if the value being
converted can be represented exactly in the new type, it is unchanged.
If the value being
converted is in the range of values that can be represented but cannot
be represented exactly, the result is either the nearest higher or
nearest lower representable value, chosen
in an implementation-defined manner. If the value being converted is
outside the range of
values that can be represented, the behavior is undefined.


since it says about a situation in which "value being converted can be
represented exactly in the new type" but does not say anything about
the requirements for it to be represented then it may mean that
sometimes the value can be represented and sometimes not???

On any particular implementation, a particular integer
value is either always or never representable as a floating-
point (float, double, long double) value. A specific integer
value doesn't waver between "sometimes representable, sometimes
not;" it's either representable or not representable, always.
why should anything like this be stated "if the value being converted
can be represented exactly in the new type"

Because some integer values may be unrepresentable in
floating-point form. You may need to experiment a little to
find such integer values on your system, but here's a good
candidate:

long l = (LONG_MAX ^ (LONG_MAX >> 1)) + 1;
float f = l;
printf ("l = %ld, f = %f\n", l, f);
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top