473,544 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

invalid floating point value

OK, I realize that what I am asking here is not likely to have a
answer within the C standard. Nonetheless, it is not specific
to any particular platform, so I'll hazard the question anyway.

A C double can be aliased by an array of unsigned char, to which
any desired byte pattern may be written. Is there any somewhat
portable way of assigning a byte pattern such that the result
will, with very high probability, _not_ count as a valid double?

I ask because I'm in search of a mechanism for flagging "missing
values" in an array of doubles. I'm aware that the "best" (fully
portable) way probably involves setting up an ancillary array, or
making the basic data-type not a straight double but a struct of
some sort. But in context this would be a royal pain. One
common solution is to flag "missing" with a specific value such
as -999.0. I'm wondering if that can be improved upon without
resorting to a wider data type.

Allin Cottrell
Wake Forest University
Nov 14 '05 #1
21 4183
>OK, I realize that what I am asking here is not likely to have a
answer within the C standard. Nonetheless, it is not specific
to any particular platform, so I'll hazard the question anyway.

A C double can be aliased by an array of unsigned char, to which
any desired byte pattern may be written. Is there any somewhat
portable way of assigning a byte pattern such that the result
will, with very high probability, _not_ count as a valid double?


NO.

However, many implementations use IEEE floating point, which has a
oddball value (or set of values) called a NaN (Not A Number). If
it is available on your implementation, this is a reasonable candidate
for an invalid value. Further, arithmetic with a NaN almost always
results in a NaN.

double x;
... put a value in x ...;
if (x != x ) { ... you have a NaN here ... ; }
(some compilers might get this wrong, though.)

Some ways to get hold of a NaN include 0.0/0.0 and log(0.0). Note
that the "most portable" way does NOT involve overlaying a double
with bytes and stuffing something into the bytes.

Other possibilities include using a very large positive or negative
value, which cannot possibly be a valid value. For example, 1.0e30
is likely not a valid value for an electric bill balance, but it
might get you in the news if you mistakenly send one out for that
amount anyway. 1.0e30 is also within the ANSI C minimum range for
float and double.

Gordon L. Burditt
Nov 14 '05 #2
OK, I realize that what I am asking here is not likely to have a
answer within the C standard. Nonetheless, it is not specific
to any particular platform, so I'll hazard the question anyway.

A C double can be aliased by an array of unsigned char, to which
any desired byte pattern may be written. Is there any somewhat
portable way of assigning a byte pattern such that the result
will, with very high probability, _not_ count as a valid double?
NO.

However, many implementations use IEEE floating point, which has a
oddball value (or set of values) called a NaN (Not A Number). If
it is available on your implementation, this is a reasonable candidate
for an invalid value. Further, arithmetic with a NaN almost always
results in a NaN.


Yep. Import is the *almost*; can give you some cheesy errors.

Nice story on the side: A colleague of mine once had the problem that
a "matrix" he was working with suddenly seemed only able to hold 0.0
as values. After searching for some time he came to me and we went
through with the debugger, finding that at one particular point the
"matrix" (still filled with zeros) scaled with 1/inf and all the zeros
got into a sort of NaN state but did not print as NaNs as all other NaNs
would have...
Not the same problem, I know, but I think it is good to know that
these things still can happen with IEEE FPs...

double x;
... put a value in x ...;
if (x != x ) { ... you have a NaN here ... ; }
(some compilers might get this wrong, though.)

Some ways to get hold of a NaN include 0.0/0.0 and log(0.0). Note
that the "most portable" way does NOT involve overlaying a double
with bytes and stuffing something into the bytes.
Or you can use the isnan() macro from <math.h>
(I am not sure at the moment whether this is only there in C99 or not)

Other possibilities include using a very large positive or negative
value, which cannot possibly be a valid value. For example, 1.0e30
is likely not a valid value for an electric bill balance, but it
might get you in the news if you mistakenly send one out for that
amount anyway. 1.0e30 is also within the ANSI C minimum range for
float and double.


On another note: If you only want to flag that there is some
invalid value out there, you can use an errno-like mechanism and
check in a test mode after every assignment, perhaps even aborting
the program after throwing out the necessary info using assert(0).
Easier to do in a portable way.

If you really want to use you actual double values and are fairly
sure that you will only use IEEE doubles, produce a NaN and then
write to the lower 32 Bit of the mantissa whatever you think
befitting to signal the specific condition, e.g. 0xdeadbeef.
However, this information might not get to you as expected if there
are intermediate steps where the value gets loaded into some register
and written back.
Cheers
Michael

Nov 14 '05 #3
Allin Cottrell wrote:

OK, I realize that what I am asking here is not likely to have a
answer within the C standard. Nonetheless, it is not specific
to any particular platform, so I'll hazard the question anyway.

A C double can be aliased by an array of unsigned char, to which
any desired byte pattern may be written. Is there any somewhat
portable way of assigning a byte pattern such that the result
will, with very high probability, _not_ count as a valid double?

I ask because I'm in search of a mechanism for flagging "missing
values" in an array of doubles. I'm aware that the "best" (fully
portable) way probably involves setting up an ancillary array, or
making the basic data-type not a straight double but a struct of
some sort. But in context this would be a royal pain. One
common solution is to flag "missing" with a specific value such
as -999.0. I'm wondering if that can be improved upon without
resorting to a wider data type.


Instead of -999.0, use either HUGE_VAL or -HUGE_VAL, from math.h.

--
pete
Nov 14 '05 #4
In <cj***********@ f1n1.spenet.wfu .edu> Allin Cottrell <co******@wfu.e du> writes:
A C double can be aliased by an array of unsigned char, to which
any desired byte pattern may be written. Is there any somewhat
portable way of assigning a byte pattern such that the result
will, with very high probability, _not_ count as a valid double?
Before IEEE 754, typical floating point representations had no invalid
bit patterns. A single rule was used for interpreting any bit pattern
(with the possible exception of all bits 0, which could be interpreted
as an exact representation of 0.0, regardless of the rule).
I ask because I'm in search of a mechanism for flagging "missing
values" in an array of doubles. I'm aware that the "best" (fully
portable) way probably involves setting up an ancillary array, or
making the basic data-type not a straight double but a struct of
some sort. But in context this would be a royal pain. One
common solution is to flag "missing" with a specific value such
as -999.0. I'm wondering if that can be improved upon without
resorting to a wider data type.


If you can afford in-band signalling, by all means, use it. DBL_MIN and
DBL_MAX are the most likely a priori candidates.

OTOH, if you can afford assuming IEEE 754 floating point (the PDP-11 is
dead, the VAX is dead, IBM mainframes might still be posing a problem)
you can choose Inf or NaN for this purpose. C99 has methods for testing
for them (isinf, isnan), but I don't know how portable they are to C89
implementations . Ditto for isfinite, which checks that a floating point
value is neither infinite nor a NaN.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #5
In article <cj**********@s unnews.cern.ch> Da*****@cern.ch (Dan Pop) writes:
....
Before IEEE 754, typical floating point representations had no invalid
bit patterns. A single rule was used for interpreting any bit pattern
(with the possible exception of all bits 0, which could be interpreted
as an exact representation of 0.0, regardless of the rule).


What were typical floating point representations ? I know that the
CDC Cyber, Cray 1, Vax and Gould all had a floating point bit pattern
that would never be generated by a valid operation, except perhaps on
overflow or things like that.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #6
In <I4********@cwi .nl> "Dik T. Winter" <Di********@cwi .nl> writes:
In article <cj**********@s unnews.cern.ch> Da*****@cern.ch (Dan Pop) writes:
...
Before IEEE 754, typical floating point representations had no invalid
bit patterns. A single rule was used for interpreting any bit pattern
(with the possible exception of all bits 0, which could be interpreted
as an exact representation of 0.0, regardless of the rule).


What were typical floating point representations ? I know that the
CDC Cyber, Cray 1, Vax and Gould all had a floating point bit pattern
that would never be generated by a valid operation, except perhaps on
overflow or things like that.


I'm not aware of any bit pattern that wouldn't represent a valid value for
the IBM 360, PDP-11 and the VAX. Ditto for certain representations used
on systems with no floating point hardware support.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #7
Da*****@cern.ch (Dan Pop) writes:
[...]
OTOH, if you can afford assuming IEEE 754 floating point (the PDP-11 is
dead, the VAX is dead, IBM mainframes might still be posing a problem)
you can choose Inf or NaN for this purpose. C99 has methods for testing
for them (isinf, isnan), but I don't know how portable they are to C89
implementations . Ditto for isfinite, which checks that a floating point
value is neither infinite nor a NaN.


The VAX isn't entirely dead; I have an account on one running OpenVMS
6.2. At my previous job, we had a mix of VAX and Alpha boxes running
production code under OpenVMS, but they may have switched over to just
Alphas since then. (The Alpha, which is also approaching the end of
its lifetime, supports both VAX and IEEE floating-point formats.)

Cray has its own non-IEEE floating-point format as well, but most
(all?) of their newer systems use IEEE.

My knowledge is incomplete, but I suspect that IBM mainframes are the
last major holdout of non-IEEE floating-point.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
In article <cj**********@s unnews.cern.ch> Da*****@cern.ch (Dan Pop) writes:
In <I4********@cwi .nl> "Dik T. Winter" <Di********@cwi .nl> writes:
In article <cj**********@s unnews.cern.ch> Da*****@cern.ch (Dan Pop) writes:
...
Before IEEE 754, typical floating point representations had no invalid
bit patterns. A single rule was used for interpreting any bit pattern
(with the possible exception of all bits 0, which could be interpreted
as an exact representation of 0.0, regardless of the rule).


What were typical floating point representations ? I know that the
CDC Cyber, Cray 1, Vax and Gould all had a floating point bit pattern
that would never be generated by a valid operation, except perhaps on
overflow or things like that.


I'm not aware of any bit pattern that wouldn't represent a valid value for
the IBM 360, PDP-11 and the VAX. Ditto for certain representations used
on systems with no floating point hardware support.


Note that I wrote "not generated", not "invalid". In IEEE there are also
no invalid bitpatterns. But whatever, from the PDP11/04/34/45/55/60
processor hndbook, 1978-1979, DEC 1978, page 250 (also valid for the VAX):
"The Undefined Variable
The undefined variable is any bit pattern with a sign bit of one and a
biased exponent of zero. The term undefined variable is used to
indicate that these bit patterns are not assigned a corresponding
floating point arithmetic value. An undefined variable is frequently
referred to as '-0' elsewhere in this chapter."
I do not have an IBM POP, but I think it will also generate only one
form of zero as the result of an operation, so that machine has also
many bit patterns that are not generated and can be used to indicate
an undefined variable. And I think that most software implementations
also represent 0.0 in a single way out of the many choices. Actually
I do only know two machines that have *no* pattern that cannot be
generated. One is the Electrologica X8 that prefers to generate -0.0,
but on occasion generates +0.0, and an old ICL machine that had no
representation for 0.0 (also a joy of course).
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #9
pete wrote:
Allin Cottrell wrote:

I ask because I'm in search of a mechanism for flagging "missing
values" in an array of doubles. I'm aware that the "best" (fully
portable) way probably involves setting up an ancillary array, or
making the basic data-type not a straight double but a struct of
some sort. But in context this would be a royal pain. One
common solution is to flag "missing" with a specific value such
as -999.0. I'm wondering if that can be improved upon without
resorting to a wider data type.

Instead of -999.0, use either HUGE_VAL or -HUGE_VAL, from math.h.


Thank to all for the suggestions. It seems that the above, or
DBL_MAX/DBL_MIN are probably the best bets for my situation.

Allin Cottrell
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
11494
by: Simon Reye | last post by:
Is there a way to force a floating point value to be invalid? I'm reading floating points from a device and every now and then I get an invalid number, but it is too few and far between for me to be able to test exception handling properly. I want to be able to force an invalid floating point value so I can test it. I can put what ever hex...
10
6694
by: Vinny | last post by:
I have a few floating point questions/issues I wish to ask. An answer or discussion would be nice too :) Any links discussion the subject would be nice too.. 1. How do I generate an underflow exception for testing purposes? 2. What is the fastest wat to suppress floating point exceptions? Take the following situation as example. float...
5
3728
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are using the Debug-Version of the program, but it fails (or leads to false results) if we are using the Release-Version. After a long debugging...
10
3335
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does anybody know how I could do this?
6
1984
by: aegis | last post by:
how can I take the fractional part of a floating point value such that I can store that value into an integer type? float foo = 6.180; now take the fractional part such that you can store 180 into an object of type int or any other integer type. I'm looking for way not involving the use of
13
7763
by: tings | last post by:
An article states: "In floating point maths, where if you divide by a sufficiently large number sufficiently often, you will always be able to reach a value too small to distinguish from zero, given the finite precision you have." What happens when "a value too small to distinguish from zero" is reached by the above division? Underflow? ...
2
3148
by: marko.suonpera | last post by:
I'm using the Newmat library where element access in ColumnVector type is defined as follows: typedef double Real; Real& ColumnVector::operator()(int m) { REPORT if (m<=0 || mnrows) Throw(IndexException(m,*this)); return store;
4
1807
by: mathieu | last post by:
Hi, I am working on an IO library, and I am reading a binary blob which represent a floating point. I would like to know what is the correct way to interpret it. For integer type using a stringstream works like a charm, but for floating point I am getting a behavior that I don't quite understand, for instance (*). BTW NaN is the IEEE with...
7
2356
by: pcauchon | last post by:
When I try to run this code on my machine (iMac with MacOS 10.5), I get very strange results. I am using this compiler: i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465) The first program loop gives the results I expect (the value of 2**x when x is smaller than -1024 should be smaller than 10**-308). However the second loop...
0
7417
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7361
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7601
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7701
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5289
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1834
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
983
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
654
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.