Violation of Conversion rule of C99.

W

William Hughes

I've noted that compilers (usually in some strict mode) even emit
warnings for comparing with 0.0. I have enough faith in implementors
that that seems too much of a stretch. Not to mention that there is no
other way to check for division-by-zero.

Well, there is no easy way to tell if a comparison with zero
is to avoid an exception (e.g. divide by 0) or
an implicit equality comparison (f1-f2)==0.
Still, a warning that warns agains a needed construct
with no good altenate form is (to say the least)
questionable.

I also think it will be
generally safe to compare with 1.0 (based on typical floating point
implementations).

It is always safe. It may even make sense at times. Although
not guaranteed, you can expect integers to be exaclty represented
and exactly converted. So

if( (float)1 == 1.0 )

will probably do what you expect. It is still however stupid.

The more likely case is

a: if ( f1 == 1.0 )

This is (probably) equivalent to

b: if( (floor(f1) == 1.0) && (ceil(f1) == 1.0))

If you know enough about f1 to know that b will work then
you can use a. If you do not know that b will work then you do not
know that
a will work.


- William Hughes
 
R

Richard Bos

William Hughes said:
It is always safe. It may even make sense at times. Although
not guaranteed, you can expect integers to be exaclty represented
and exactly converted. So

if( (float)1 == 1.0 )

will probably do what you expect. It is still however stupid.

Actually said:
The more likely case is

a: if ( f1 == 1.0 )

This is (probably) equivalent to

b: if( (floor(f1) == 1.0) && (ceil(f1) == 1.0))

Yes, in the sense that if f1 is very close to 1.0, it will probably
compare equal to 1.0. No, in the sense that if f1 was last set to 1, it
must still compare equal to 1.0, with no probably about it.

Richard
 
W

William Hughes

Actually, it must. See FLT_DIG in <float.h>.

If (and this is indeed unlikely) the floating point system does not
have exact representations for integers, then I cannot see why
the cast to float of the integer 1 should have to equal the double
constant 1.0.
Or does the standard mandate exact representations for integers?
> The more likely case is




Yes, in the sense that if f1 is very close to 1.0, it will probably
compare equal to 1.0. No, in the sense that if f1 was last set to 1, it
must still compare equal to 1.0, with no probably about it.


The question is not "when will f1 compare equal to 1.0?", but "when
is a equivalent to b?". Assume that 1.0 is accurately represented,
but floor(1.0) and ceil(1.0) both return values very slightly greater
than
1.0 (very poor QOI but the standard does not set accuracy limits on
library functions).


- William Hughes
 
R

Richard Bos

William Hughes said:
If (and this is indeed unlikely) the floating point system does not
have exact representations for integers, then

....it is not a C implementation.
Or does the standard mandate exact representations for integers?

See the explanation of FLT_DIG in the Standard.

Richard
 
W

William Hughes

...it is not a C implementation.


See the explanation of FLT_DIG in the Standard.

Are you referring to


number of decimal digits, q, such that any floating-
point number with q decimal digits can be rounded into
a floating-point number with p radix b digits and back
again without change to the q decimal digits,

plog10b if b is a power of 10

|(p-1)log10b|otherwise

FLT_DIG 6


This certainly requires invertability, so there must be a float
value,x, which represents the real number 1.0. Futhermore
1.0 must "round" to this value. Also there must be a double value,
y,
which represents, 1.0 and 1.0 must "round" to this number. However,
I do not see why we need to assume that x is equal to y is equal to
1.0 unless we read the standard to insist that the floating point
numbers must be fixed point with respect to some base.
This would certainly seem to be implied by:

The characteristics of floating types are defined in
terms of a model that describes a representation of
floating-point numbers and values that provide information
about an implementation's floating-point arithmetic.14) The
following parameters are used to define the model for each
floating-point type:

s sign (1)
b base or radix of exponent representation (integer > 1)
e exponent (integer between a min emin and a max emax)
p precision (number of base-b digits in the significand)
fk nonnegative integers less than b (significand digits)

But

The floating-point model is intended to clarify the
description of each floating-point characteristic and
does not require the floating-point arithmetic of the
implementation to be identical.

might be interpreted in such a way as to give a perverse
implementation
wiggle room.

- William Hughes
 

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top