473,396 Members | 1,590 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,396 software developers and data experts.

A few floating point issues

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 x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

z = x / y; // this should generate a overflow exception

I could use exception handling like so:
float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

try {
z = x / y; // this should generate a overflow exception
}
catch( EOverflow &eo ) {
//do anything
z = 1.0e38;
}
catch( EZeroDivide &ez ) {
//do anything
z = 1.0e38;
}
catch( EUnderflow &eu ) {
//do anything
z = 0.0;
}
Or is there a better method?

3. This one is weird and may be my compiler itself. When i use the next
example (same as the first one) and compile it for debugging or release i
sometimes get an exception and sometimes i don't. If it didn't and i was
in debug mode, I notice the value of z was unchanged. No matter what the
"useless assignment"'s value was.

float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

z = x / y; // this should generate a overflow
Thanks in advance..
Greetz,
Vinny
Jul 22 '05 #1
10 6686
Vinny wrote:
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?
I don't think there is a certain way, but you could try...

Divide a small number by a big number, like

double a = 1e-200;
double b = 1e200;
double ab = a / b;
2. What is the fastest wat to suppress floating point exceptions?
The language doesn't have any means to do that. They are platform-
specific, I believe.
Take the following situation as example.
float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

z = x / y; // this should generate a overflow exception
Hopefully.
I could use exception handling like so:
float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

try {
z = x / y; // this should generate a overflow exception
}
catch( EOverflow &eo ) {
//do anything
z = 1.0e38;
}
catch( EZeroDivide &ez ) {
//do anything
z = 1.0e38;
}
catch( EUnderflow &eu ) {
//do anything
z = 0.0;
}
I don't think so. What's EOverflow? What's EZeroDivide? Those are
not Standard types.
Or is there a better method?
You'd have to ask in a newsgroup for your compiler.
3. This one is weird and may be my compiler itself. When i use the next
example (same as the first one) and compile it for debugging or release i
sometimes get an exception and sometimes i don't. If it didn't and i was
in debug mode, I notice the value of z was unchanged. No matter what the
"useless assignment"'s value was.

float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

z = x / y; // this should generate a overflow


Again, the language doesn't really have any way of capturing hardware
exceptions like floating point errors. Perhaps the documentation for your
compiler has some information...

V
Jul 22 '05 #2
Victor Bazarov <v.********@comAcast.net> wrote in
news:Nr****************@newsread1.dllstx09.us.to.v erio.net:
Vinny wrote:
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?
I don't think there is a certain way, but you could try...

Divide a small number by a big number, like

double a = 1e-200;
double b = 1e200;
double ab = a / b;


I kinda tried that. Didn't help.

2. What is the fastest wat to suppress floating point exceptions?


The language doesn't have any means to do that. They are platform-
specific, I believe.
Take the following situation as example.
float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

z = x / y; // this should generate a overflow exception


Hopefully.
I could use exception handling like so:
float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

try {
z = x / y; // this should generate a overflow
exception
}
catch( EOverflow &eo ) {
//do anything
z = 1.0e38;
}
catch( EZeroDivide &ez ) {
//do anything
z = 1.0e38;
}
catch( EUnderflow &eu ) {
//do anything
z = 0.0;
}


I don't think so. What's EOverflow? What's EZeroDivide? Those are
not Standard types.


Sorry. This was from testing in BCB6. But i assumed catching those
exceptions are possible on other compilers as well and people would get
the idea :) But assuming it wasn't borland. There must be a way to catch
those acceptions? or am i wrong?
Or is there a better method?


You'd have to ask in a newsgroup for your compiler.
3. This one is weird and may be my compiler itself. When i use the
next example (same as the first one) and compile it for debugging or
release i sometimes get an exception and sometimes i don't. If it
didn't and i was in debug mode, I notice the value of z was
unchanged. No matter what the "useless assignment"'s value was.

float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

z = x / y; // this should generate a overflow


Again, the language doesn't really have any way of capturing hardware
exceptions like floating point errors. Perhaps the documentation for
your compiler has some information...

V


So i guess the exception handling is compiler specific. I kinda guess
your write. *Getting flashbacks of some nasty assembly experiences with
division by zero errors*

Jul 22 '05 #3
>>
I don't think so. What's EOverflow? What's EZeroDivide? Those are
not Standard types.


Sorry. This was from testing in BCB6. But i assumed catching those
exceptions are possible on other compilers as well and people would get
the idea :) But assuming it wasn't borland. There must be a way to catch
those acceptions? or am i wrong?


What you are wrong about is assuming that these exception exist anywhere
other than in Borland. C++ defines no standard means of dealling with
floating point exceptions at all. It does not even define that floating
point exceptions exist. It is all compiler specific. Perhaps you should ask
on a Borland newsgroup.

john
Jul 22 '05 #4
[snip]

On most plattforms you need to activate FPU-Exceptions first. For windows
you need

_control87( 0, _EM_INVALID | _EM_DENORMAL | _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW);
_clearfp();

somewhere before you try to generate an exception.

Jul 22 '05 #5
Vinny wrote:
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..
First off, there's bound to be some confusion here: floating point
"exceptions" in the Standard C library sense - technically the IEEE 754
standard - are not (necessarily) "exceptions" in the C++ sense.

I'm not sure exactly what the IEEE 754 standard says, but generally
under an IEEE 754 exception a floating point "status word" will be set
to indicate the problem. How you access this status word will almost
certainly be compiler/system-dependent.

On some sytems you may be able to set "traps" for floating-point
exceptions; this should cause a SIGFPE signal to be raised (the default
action of which, if not handled, is probably to abort your program) in
the case of an IEEE 754 fp exception.

On some systems (yours, by the looks of it) your C++ compiler might be
persuaded to throw C++ exceptions in the case of IEEE 754 fp
exceptions.
1. How do I generate an underflow exception for testing purposes?
This is - as are many floating point issues - probably
architecture/compiler dependent. However, you could try:

#include <cmath>

std::exp(-1000.0);

This certainly *should* cause underflow (!)
2. What is the fastest wat to suppress floating point exceptions?
Not sure quite what you mean by "supress", but see above... if you're
not trapping exceptions or throwing C++ exceptions, then by default
nothing happens apart from the fp status word being set (which you are
free to ignore). Your compiler might even allow you to "turn off" the
fp status word setting (although this would break some standards
compliance, I guess). Gnu gcc, for example, allows this as an
optimisation option, IIRC.
Take the following situation as example.
float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

z = x / y; // this should generate a overflow exception

I could use exception handling like so:
float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

try {
z = x / y; // this should generate a overflow exception
}
catch( EOverflow &eo ) {
//do anything
z = 1.0e38;
}
catch( EZeroDivide &ez ) {
//do anything
z = 1.0e38;
}
catch( EUnderflow &eu ) {
//do anything
z = 0.0;
}
I don't know what your compiler/system are, and have no idea what the
(apparently C++) exceptions EOverflow, etc. are. This is not standard
C++.
Or is there a better method?
See above - check if your compiler allows you to turn off fp exception
handling.
3. This one is weird and may be my compiler itself. When i use the next example (same as the first one) and compile it for debugging or release i sometimes get an exception and sometimes i don't. If it didn't and i was in debug mode, I notice the value of z was unchanged. No matter what the "useless assignment"'s value was.

float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

z = x / y; // this should generate a overflow


Be interested to know what your system/compiler are, and what compiler
flags are set for "release" and "debug".

Regards,

--
Lionel B

Jul 22 '05 #6
"Lionel B" <go****@lionelb.com> wrote in news:1098873163.334557.25440
@f14g2000cwb.googlegroups.com:
3. This one is weird and may be my compiler itself. When i use the

next
example (same as the first one) and compile it for debugging or

release i
sometimes get an exception and sometimes i don't. If it didn't and i

was
in debug mode, I notice the value of z was unchanged. No matter what

the
"useless assignment"'s value was.

float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

z = x / y; // this should generate a overflow


Be interested to know what your system/compiler are, and what compiler
flags are set for "release" and "debug".

Regards,


Read my reactor to Victor. It contains the answers you request :)

Compiler = BCB6
the example code was within a simpel onclick event. But it was just an
example :) If other compilers work fine with the code i guess i must have
made a stupid mistake somewhere.

Greetz
Vincent
Jul 22 '05 #7
"Real Name" <re@alna.me> wrote in news:41********@news.arcor-ip.de:
[snip]

On most plattforms you need to activate FPU-Exceptions first. For
windows you need

_control87( 0, _EM_INVALID | _EM_DENORMAL | _EM_ZERODIVIDE |
_EM_OVERFLOW | _EM_UNDERFLOW); _clearfp();

somewhere before you try to generate an exception.


I recognize this code from some OpenGL code I once was trying.
But if there is a way to activate them, is there not a way to deactivate
them?

Greetz
Vincent
Jul 22 '05 #8
"Real Name" <re@alna.me> wrote in news:41********@news.arcor-ip.de:
_control87( 0, _EM_INVALID | _EM_DENORMAL | _EM_ZERODIVIDE |
_EM_OVERFLOW | _EM_UNDERFLOW); _clearfp();


Ok, I found this on the INet. It says borland but is this also compiler
specific?
Greetz Vincent

---------------------------------------------------------------------------
EXCEPTIONS

Math exceptions and access violations will occasionally cause your OpenGL
applications to abort. By default, MS ignores floating point exceptions;
Borland does not. Add the following line to the initialization section of
your code:

_control87(MCW_EM, MCW_EM); /* defined in float.h */

It will disable Borland's floating point exception handlers.

Windows 9x applications may need to call

_clear87();
_control87(MCW_EM, MCW_EM); /* defined in float.h */

before each frame is rendered.

---------------------------------------------------------------------------
Jul 22 '05 #9
Vinny wrote:
"Real Name" <re@alna.me> wrote in news:41********@news.arcor-ip.de:
_control87( 0, _EM_INVALID | _EM_DENORMAL | _EM_ZERODIVIDE |
_EM_OVERFLOW | _EM_UNDERFLOW); _clearfp();

Ok, I found this on the INet. It says borland but is this also compiler
specific?


Yes, it is. Quite. 'borland.public.cppbuilder.language' awaits you.

V
Jul 22 '05 #10
Ok.. Thanks everyone.. Didn't know all of it was so compiler specific..

I learned alot about floating points ( and my compiler :D )

Vincent
Jul 22 '05 #11

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

Similar topics

4
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can...
5
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...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
3
by: Mark L Pappin | last post by:
<puts on Compiler Vendor hat> I've recently discovered that our compilers don't make any attempt to handle floating point overflow in add/subtract/ multiply/divide, with the result that...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
9
by: David Veeneman | last post by:
I'm working on a project that uses floating-point values (doubles), and I'm being driven crazy by something pretty basic. I understand that it's in the nature of floating-point calculations to...
33
by: dis_is_eagle | last post by:
hi....i have encountered strange problem regarding floating point comparison...the problem is... main() { float a=0.7; if(0.7 a) printf("hi"); else printf("hello");
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
5
by: Keflavich | last post by:
Hey, I have a bit of code that died on a domain error when doing an arcsin, and apparently it's because floating point subtraction is having problems. I know about the impossibility of storing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.