Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

comparing to HUGE_VAL

0 views
Skip to first unread message

Oleg Goldshmidt

unread,
Dec 24, 1997, 3:00:00 AM12/24/97
to

Hi everybody,

How does one validate the value returned by strtod()? The function
returns HUGE_VAL with the proper sign in case of overflow (see K&R
Appendix B6). How should one check for overflow in a
portable way (multiplatform, any ANSI-compliant compiler)?

In other words, with the declarations (and assuming I include the
necessary stuff)

const char* s;
char* endp;
double value;

I want to write

value = strtod(s, &endp);
if (overflow)
do_some_error_handling();

What should the overflow condition be:

1) value == HUGE_VAL
2) value >= HUGE_VAL
3) something else

(With the corresponding conditions for negative values, of course).

Please CC to gol...@netvision.net.il.

Thanks,

--
Oleg Goldshmidt | Disclaimer: My employer has no
Berger Financial Research Ltd. | opinion on this matter. At least
gold...@netvision.net.il | none I am aware of.
-----------------------------------------------------------------

Alicia Carla Longstreet

unread,
Dec 24, 1997, 3:00:00 AM12/24/97
to Oleg Goldshmidt

Oleg Goldshmidt wrote:

> Hi everybody,

> How does one validate the value returned by strtod()? The function
> returns HUGE_VAL with the proper sign in case of overflow (see K&R
> Appendix B6). How should one check for overflow in a
> portable way (multiplatform, any ANSI-compliant compiler)?

> In other words, with the declarations (and assuming I include the
> necessary stuff)

> const char* s;
> char* endp;
> double value;

> I want to write

> value = strtod(s, &endp);
> if (overflow)
> do_some_error_handling();

> What should the overflow condition be:

How about:
if( errno == ERANGE )

If strtod() cannot place the result in a double, e.g. the value is
greater than the largest possible value, less then the 'largest'
negative value, or to small (too close to zero), the function will set
errno to ERANGE. At this point, you can check the following:

if( value == HUGE_VAL ) {...}
else if( value == -(HUGE_VAL ) {...}
else /* value == 0 */ {...}

You do need to check errno to determine if 0 is an 'error value' or the
actual value of the string passed.

> 1) value == HUGE_VAL
> 2) value >= HUGE_VAL
> 3) something else

1) value == HUGE_VAL



> (With the corresponding conditions for negative values, of course).

> Please CC to gol...@netvision.net.il.

--
****************************************************************
Where are Preparations A through G?
Are there seeing eye humans for blind dogs?
If knees were backwards, what would chairs look like?
When your pet bird sees you reading the newspaper,
does he wonder why you're just sitting there, staring at carpeting?
What happened to the first 6 "ups"?
=========================================
Alicia Carla Longstreet ca...@ici.net
=========================================
READ THE FAQ for more information:
C-FAQ ftp sites: ftp://ftp.eskimo.com or ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Oleg Goldshmidt

unread,
Dec 25, 1997, 3:00:00 AM12/25/97
to

Oleg Goldshmidt <gold...@netvision.net.il> writes:
> How does one validate the value returned by strtod()? The function
> returns HUGE_VAL with the proper sign in case of overflow (see K&R
> Appendix B6). How should one check for overflow in a
> portable way (multiplatform, any ANSI-compliant compiler)?

Thanks to the kind souls who replied to me by posting or private
emails. Basically, everybody pointed out that strtod() sets errno =
ERANGE and it should be &&-ed with value == HUGE_VAL etc.

I asked a specific strtod()-related question because I didn't want too
many replies like "If you mean such-and-such situation then do
this..." I guess that was a miscalculation... While the above is a
solution for the strtod() example, I am also interested in a more
general question of how to deal with overflows. Not every overflow
sets errno to any useful value, I guess (or is there a standard that
mandates something like this?). So how would I check that a double
value is not an Inf (HUGE_VAL is Inf on my i386 linux 2.0.30 +
gcc-2.7.2.1-2 + libc-5.3.12-17 system, but I am interested in a
portable solution) as a result of division by zero or anything else,
but is a legal value.

E.g. suppose I have a function that accepts a double argument,
how do I validate that a caller passed a legal value? Is

value == HUGE_VAL || value == -HUGE_VAL

the proper conditional? I am not sure, HUGE_VAL _is_ a legal value.
On the other hand, I am not sure that

value > HUGE_VAL

or

value >= HUGE_VAL

is legal and/or proper.

Thanks,

Oleg Goldshmidt
Berger Financial Research Ltd.
gold...@netvision.net.il

Alex Krol

unread,
Dec 25, 1997, 3:00:00 AM12/25/97
to Oleg Goldshmidt

Oleg Goldshmidt wrote:
>
> Hi everybody,
>
> How does one validate the value returned by strtod()? The function
> returns HUGE_VAL with the proper sign in case of overflow (see K&R
> Appendix B6). How should one check for overflow in a
> portable way (multiplatform, any ANSI-compliant compiler)?
>
> In other words, with the declarations (and assuming I include the
> necessary stuff)
>
> const char* s;
> char* endp;
> double value;
>
> I want to write
>
> value = strtod(s, &endp);
> if (overflow)
> do_some_error_handling();
>
> What should the overflow condition be:
>
> 1) value == HUGE_VAL
> 2) value >= HUGE_VAL
> 3) something else
>
> (With the corresponding conditions for negative values, of course).
>

man strtod
...If the conversion causes an overflow,... +/- HUGE_VAL
is returned with the sign indicating the direction of the
overflow, and the errno global variable is set to ERANGE
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

So, check first errno (it is set to ERANGE in case of an
overflow and an underflow), and after that - check return value
(if it is equal to HUGE_VAL, -HUGE_VAl or, in case of underflow, 0)

Merry Hanuka and Christmas.

Regards,
Alex Krol

Alicia Carla Longstreet

unread,
Dec 25, 1997, 3:00:00 AM12/25/97
to Oleg Goldshmidt

Oleg Goldshmidt wrote:


> Oleg Goldshmidt <gold...@netvision.net.il> writes:
> > How does one validate the value returned by strtod()? The function
> > returns HUGE_VAL with the proper sign in case of overflow (see K&R
> > Appendix B6). How should one check for overflow in a
> > portable way (multiplatform, any ANSI-compliant compiler)?

> Thanks to the kind souls who replied to me by posting or private
> emails. Basically, everybody pointed out that strtod() sets errno =
> ERANGE and it should be &&-ed with value == HUGE_VAL etc.

That will handle overflow but not necessarily underflow. When using the
standard functions just check errno and if it is equal to ERANGE then,
and only then, should you bother to check against HUGE_VAL, -HUGE_VAL,
or zero.



> I asked a specific strtod()-related question because I didn't want too
> many replies like "If you mean such-and-such situation then do
> this..." I guess that was a miscalculation... While the above is a
> solution for the strtod() example, I am also interested in a more
> general question of how to deal with overflows. Not every overflow
> sets errno to any useful value,

All of the standard functions that deal with doubles do. When working
with integer values or float's you will have to take care to check for
overflow function.

> I guess (or is there a standard that
> mandates something like this?). So how would I check that a double
> value is not an Inf (HUGE_VAL is Inf on my i386 linux 2.0.30 +
> gcc-2.7.2.1-2 + libc-5.3.12-17 system, but I am interested in a
> portable solution) as a result of division by zero or anything else,
> but is a legal value.

And every other conforming C implementation.



> E.g. suppose I have a function that accepts a double argument,
> how do I validate that a caller passed a legal value? Is
>
> value == HUGE_VAL || value == -HUGE_VAL

HUGE_VAL is part of Standard C. The above comparison is correct and
portable.



> the proper conditional? I am not sure, HUGE_VAL _is_ a legal value.
> On the other hand, I am not sure that
>
> value > HUGE_VAL

Since HUGE_VAL is the largest possible value doing the above comparison
will always be false. value can never be larger than HUGE_VAL.

> or
>
> value >= HUGE_VAL

This is fine but since value can never be larger than HUGE_VAL this is
pointless.


--
****************************************************************
Where are Preparations A through G?
Are there seeing eye humans for blind dogs?
If knees were backwards, what would chairs look like?
When your pet bird sees you reading the newspaper,
does he wonder why you're just sitting there, staring at carpeting?
What happened to the first 6 "ups"?

==========================================================
Alicia Carla Longstreet ca...@ici.net
Web Page http://www.ici.net/cust_pages/carla/index.html
==========================================================

Lawrence Kirby

unread,
Dec 26, 1997, 3:00:00 AM12/26/97
to

In article <lvn2hqb...@udun.bfr.co.il>
gold...@netvision.net.il "Oleg Goldshmidt" writes:

>Hi everybody,


>
>How does one validate the value returned by strtod()? The function
>returns HUGE_VAL with the proper sign in case of overflow (see K&R
>Appendix B6). How should one check for overflow in a
>portable way (multiplatform, any ANSI-compliant compiler)?
>

>In other words, with the declarations (and assuming I include the
>necessary stuff)
>
>const char* s;
>char* endp;
>double value;
>
>I want to write
>
>value = strtod(s, &endp);
>if (overflow)
> do_some_error_handling();
>
>What should the overflow condition be:
>
>1) value == HUGE_VAL
>2) value >= HUGE_VAL
>3) something else

errno = 0;
value = strtod(s, &endp);

if (endp == s) { /* No conversion was performed */ }

else if (errno == ERANGE) {
if (value == HUGE_VAL) { Result is positive and too large */ }

else if (value == -HUGE_VAL) { Result is negative and too large */ }

else { Result is too small to represent (underflow) }
}

You must set errno before calling strtod() because strtod will only ever
set it to ERANGE; it won't set it to a different value of the conversion
succeeds.

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------


0 new messages