Double to Float
Author Message
Double to Float

I have a double which I need to store in a float.

Is this the correct test before storing.

double d;
/* stuff */

float f;

if (d > 0.0)
{
if (d > FLT_MAX || d < FLT_MIN)
return some error ;

Quote:
}

else if(d < 0.0)
{
if(d > -FLT_MIN || d < -FLT_MAX)
return Some error ;

Quote:
}

f = d;

Is this correct way or am I missing anything ?

Tue, 23 Nov 2004 05:54:54 GMT
Double to Float

Quote:
> I have a double which I need to store in a float.

> Is this the correct test before storing.

> double d;
> /* stuff */

> float f;

> if (d > 0.0)
> {
>   if (d > FLT_MAX || d < FLT_MIN)
>        return some error ;
> }
> else if(d < 0.0)
> {
>    if(d > -FLT_MIN || d < -FLT_MAX)
> return Some error ;
> }

> f = d;

> Is this correct way or am I missing anything ?

Hopefully, this is pseudocode. You forgot to include <float.h>.

Gregory Pietsch

Tue, 23 Nov 2004 06:20:34 GMT
Double to Float

Quote:

> I have a double which I need to store in a float.
> Is this the correct test before storing.

> double d;
> float f;

> if (d > 0.0)
> {
>   if (d > FLT_MAX || d < FLT_MIN)
>        return some error ;
> }
> else if(d < 0.0)
> {
>    if(d > -FLT_MIN || d < -FLT_MAX)
>         return Some error ;
> }
> f = d;

> Is this correct way or am I missing anything ?

"The correct way" depends on what your program needs.
Your comparisons against FLT_MAX detect potential overflow,
and look "normal" in the sense of "this would make sense in
many programs."  The comparisons against FLT_MIN seem a bit
odd -- not wrong, necessarily, just odd -- because you're
clearly willing to let 1.0+epsilon become 1.0f, so it
seems peculiar to object when 0.0+epsilon becomes 0.0f.
Still it's "reasonable" in the sense of "this would make
sense in at least some programs."

However, there is no One True Way: You have to make
up your mind about what you want and don't want from the
conversion *in the context of the computation* and then
code accordingly.

By the way, the fabs() function might simplify your
code somewhat.

--

Tue, 23 Nov 2004 06:31:52 GMT
Double to Float

Quote:
> Is this correct way or am I missing anything ?

You could try this:

f = (float)d;
if (f != d)
return some_error;

How correct it is depends on what kind of differences your application can
accept.

-Walter
www.digitalmars.com
free C/C++/D compilers

Tue, 23 Nov 2004 07:32:06 GMT
Double to Float
On Thu, 06 Jun 2002 23:32:06 GMT, "Walter"

Quote:

> > Is this correct way or am I missing anything ?

> You could try this:

>     f = (float)d;

The cast is totally unnecessary.  The assignment of a the value of a
double to a float performs the conversion automatically.

Quote:
>     if (f != d)
>         return some_error;

> How correct it is depends on what kind of differences your application can
> accept.

He could try it, but it invokes undefined behavior if the value of the
double is outside the range of valid values for a float.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

Tue, 23 Nov 2004 09:15:03 GMT
Double to Float

Quote:
>On Thu, 06 Jun 2002 23:32:06 GMT, "Walter"

>> > Is this correct way or am I missing anything ?

>> You could try this:

>>     f = (float)d;

>The cast is totally unnecessary.  The assignment of a the value of a
>double to a float performs the conversion automatically.

>>     if (f != d)
>>         return some_error;

>> How correct it is depends on what kind of differences your application can
>> accept.

>He could try it, but it invokes undefined behavior if the value of the
>double is outside the range of valid values for a float.

Won't the float be converted to double before the comparison is made?
There is no assignment so what would be undefined?

<<Remove the del for email>>

Tue, 23 Nov 2004 10:32:12 GMT
Double to Float

in comp.lang.c:

Quote:

> >On Thu, 06 Jun 2002 23:32:06 GMT, "Walter"

> >> > Is this correct way or am I missing anything ?

> >> You could try this:

> >>     f = (float)d;

> >The cast is totally unnecessary.  The assignment of a the value of a
> >double to a float performs the conversion automatically.

> >>     if (f != d)
> >>         return some_error;

> >> How correct it is depends on what kind of differences your application can
> >> accept.

> >He could try it, but it invokes undefined behavior if the value of the
> >double is outside the range of valid values for a float.
> Won't the float be converted to double before the comparison is made?
> There is no assignment so what would be undefined?

> <<Remove the del for email>>

Perhaps I made it confusing because I split Walter's snippet.  Here it
is back together again:

Quote:
>     f = (float)d;
>     if (f != d)
>         return some_error;

The first statement assigns the value of the double d to the float f,
with an unnecessary and redundant cast.  If the value of d is outside
the range of values a float can hold, it is this statement that
invokes the undefined behavior.

Assuming that the value is in range, or the implementation performs
some implementation-defined conversion that results in a valid float
value in f, there is nothing undefined about the comparison in the if
statement.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

Tue, 23 Nov 2004 10:43:18 GMT
Double to Float

Quote:
> On Thu, 06 Jun 2002 23:32:06 GMT, "Walter"

> > You could try this:
> >     f = (float)d;
> The cast is totally unnecessary.  The assignment of a the value of a
> double to a float performs the conversion automatically.

Yes, I know. I put it in for illustrative purposes.

Quote:
> >     if (f != d)
> >         return some_error;
> > How correct it is depends on what kind of differences your application
can
> > accept.
> He could try it, but it invokes undefined behavior if the value of the
> double is outside the range of valid values for a float.

I didn't know there was any undefined behavior. If d is too big, f gets
infinity. If d is too small, f gets 0. If d gets rounded, then f will differ
in the last bit and you'll get an inexact flag. If d is nan, then f will be
nan too, but will still compare != and get the some_error. If d is exactly
representable in f, then the compare will be false.

Tue, 23 Nov 2004 12:30:05 GMT
Double to Float

Quote:

> > On Thu, 06 Jun 2002 23:32:06 GMT, "Walter"

> > > You could try this:
> > >     f = (float)d;
> > The cast is totally unnecessary.  The assignment of a the value of a
> > double to a float performs the conversion automatically.

> Yes, I know. I put it in for illustrative purposes.

And if it wasn't there, my compiler would certainly warn "implicit narrowing
cast". I think it's useful to put it in to show it's a deliberate conversion.

--
Kevin Bracey
http://www.bracey-griffith.freeserve.co.uk/

Tue, 23 Nov 2004 17:44:07 GMT
Double to Float

Quote:
>On Thu, 06 Jun 2002 23:32:06 GMT, "Walter"

>> > Is this correct way or am I missing anything ?

>> You could try this:

>>     f = (float)d;

>The cast is totally unnecessary.  The assignment of a the value of a
>double to a float performs the conversion automatically.

>>     if (f != d)
>>         return some_error;

>> How correct it is depends on what kind of differences your application can
>> accept.

>He could try it, but it invokes undefined behavior if the value of the
>double is outside the range of valid values for a float.

Apart from that, losing some precision is to be expected and not to be
considered an error.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

Tue, 23 Nov 2004 21:07:28 GMT
Double to Float

Quote:

>> On Thu, 06 Jun 2002 23:32:06 GMT, "Walter"

>> > You could try this:
>> >     f = (float)d;
>> The cast is totally unnecessary.  The assignment of a the value of a
>> double to a float performs the conversion automatically.

>Yes, I know. I put it in for illustrative purposes.

>> >     if (f != d)
>> >         return some_error;
>> > How correct it is depends on what kind of differences your application
>can
>> > accept.
>> He could try it, but it invokes undefined behavior if the value of the
>> double is outside the range of valid values for a float.

>I didn't know there was any undefined behavior. If d is too big, f gets
>infinity.

Chapter and verse, please.

FYI:

When a double is demoted to float or a long double to double or
float, if the value being converted is outside the range of values
that can be represented, the behavior is undefined.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

Tue, 23 Nov 2004 21:11:15 GMT
Double to Float

Quote:
>I have a double which I need to store in a float.

>Is this the correct test before storing.

>double d;
>/* stuff */

>float f;

>if (d > 0.0)
>{
>  if (d > FLT_MAX || d < FLT_MIN)
>       return some error ;
>}
>else if(d < 0.0)
>{
>   if(d > -FLT_MIN || d < -FLT_MAX)
>    return Some error ;
>}

>f = d;

>Is this correct way or am I missing anything ?

In the case when the value of d is in the interval -FLT_MIN .. FLT_MIN
you may want to assign the value zero to f, rather than reporting some
error.

The usage of fabs(d) would result in a major simplification of your code.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

Tue, 23 Nov 2004 21:17:46 GMT
Double to Float

Quote:

>> > On Thu, 06 Jun 2002 23:32:06 GMT, "Walter"

>> > > You could try this:
>> > >     f = (float)d;
>> > The cast is totally unnecessary.  The assignment of a the value of a
>> > double to a float performs the conversion automatically.

>> Yes, I know. I put it in for illustrative purposes.

>And if it wasn't there, my compiler would certainly warn "implicit narrowing
>cast".

And another compiler might warn if you put it in: "perfectly useless and
potentially harmful cast".

Quote:
>I think it's useful to put it in to show it's a deliberate conversion.

What else could it be?

The cast is WRONG because it reduces the code's maintainability.  One day,
f might become a double or long double and if you forget to remove the
cast...

The compiler must trust the programmer to know what he is doing (even
when this is not the case!).  It is perfectly OK, even desirable,
for a compiler to emit a diagnostic whenever it detects an instance of
undefined behaviour, but other diagnostics not required by the standard
should not be produced, especially the ones that enforce a certain
programming style.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

Tue, 23 Nov 2004 22:52:14 GMT
Double to Float

Quote:

> >> He could try it, but it invokes undefined behavior if the value of the
> >> double is outside the range of valid values for a float.
> >I didn't know there was any undefined behavior. If d is too big, f gets
> >infinity.

> Chapter and verse, please.

Appendix F.

Wed, 24 Nov 2004 01:48:17 GMT
Double to Float

Quote:
> Apart from that, losing some precision is to be expected and not to be
> considered an error.

Depends on what the application is, of course.

Wed, 24 Nov 2004 01:48:17 GMT

 Page 1 of 2 [ 25 post ] Go to page: [1] [2]

Relevant Pages