473,324 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Generate invalid float

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 values I
want into the device but don't have a clue what hex values will create
and invalid floating point value.

Simon

Jul 19 '05 #1
3 11454

"Simon Reye" wrote:
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 values I
want into the device but don't have a clue what hex values will create
and invalid floating point value.


Use std::numeric_limits<yourfloatingpointtype> from <limits>.
You can choose between quiet_NaN(), signaling_NaN() and infinity().

double f;
f = std::numeric_limits<double>::quiet_NaN();

Be aware that NaN behave quite different than all other numbers. f!=f
returns true for example.

HTH,
Patrick
Jul 19 '05 #2
Victor Bazarov wrote:
"Simon Reye" <si*******@yahoo.com.au> wrote...
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
values I want into the device but don't have a clue what hex values
will create and invalid floating point value.


IIRC, IEEE floats are "invalid" if all bits are set. Such value
is called "Not a Number", NaN. So, if you create a file that has
only char values of -1, any attempt to read those as a float or
a double should likely get you NaN (on a two's complement system).


Or simply use std::numeric_limits<float>::quiet_NaN() or
std::numeric_limits<float>::signaling_NaN(). I don't know the
difference between those two, so I don't know if it's important which
one to use.

Jul 19 '05 #3
"Rolf Magnus" <ra******@t-online.de> wrote...
Victor Bazarov wrote:
"Simon Reye" <si*******@yahoo.com.au> wrote...
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
values I want into the device but don't have a clue what hex values
will create and invalid floating point value.


IIRC, IEEE floats are "invalid" if all bits are set. Such value
is called "Not a Number", NaN. So, if you create a file that has
only char values of -1, any attempt to read those as a float or
a double should likely get you NaN (on a two's complement system).


Or simply use std::numeric_limits<float>::quiet_NaN() or
std::numeric_limits<float>::signaling_NaN(). I don't know the
difference between those two, so I don't know if it's important which
one to use.


The difference between those is simple: the former does not cause
the hardware to generate a hardware-specific signal when reading
it from memory or storing it to memory, the latter does. If the
intention of the test is to debug the code that deals with those
things read from a stream, I'd try reading quiet_NaN first. Even
reading signaling_NaN should probably raise a signal...

BTW, an implementation does not have to provide those, to check
whether they are there, use std::numeric_limits<float>::has_XXXX
functions.

OTOH, it's still simpler to create a file with 0xFF in all bytes
to test reading those NaN values, IMHO. A hex editor is all you
need...

Victor
Jul 19 '05 #4

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

Similar topics

2
by: Tim Williams | last post by:
I'm trying to write a simple python program to access a MySQL database. I'm having a problem with using MySQLdb to get the results of a SQL command in a cursor. Sometimes the cursor.execute works,...
10
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...
15
by: John Cassidy | last post by:
This has been driving me crazy. I've done basic C in school, but my education is mainly based on object oriented design theory where Java is our tool. For some reason, while helping a friend with a...
1
by: Timbo | last post by:
Hi all, This is my first message here so i'll try and include all the information that will help you help me out, if possible. Basically I am using C# in ASP.NET 2.0 and have a Repeater...
22
by: Roberto Gori | last post by:
Hi all, i'm looking for a way, a tool or a macro like this: USEFULMACRO(foo, bar, type1 arg1, type2 arg2, ..., typeN argN) to generate the following code: void foo(type1 arg1, type2...
2
by: tkirankumar | last post by:
Hi all, uname -a SunOS cbmrsd1a1 5.10 Generic_118833-17 sun4us sparc FJSV,GPUZC-M g++ -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.10/3.3.2/specs Configured with:...
9
by: Frederic Mayot | last post by:
Hi, Can someone give me an explanation why the following code produces a bug. I think it's related to the compiler and that the first line is not OK. I have no pb with gcc 3.4 but the bug appeared...
9
by: Alan Isaac | last post by:
I need access to 2*n random choices for two types subject to a constraint that in the end I have drawn n of each. I first tried:: def random_types(n,typelist=): types = typelist*n...
3
by: triphoppa | last post by:
I'm a little confused about and error my compiler is giving me. The error message is "invalid operand to binary %." The compiler says that this code *avg = *range %10; Isn't this a valid binary...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.