473,661 Members | 2,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6711
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.********@com Acast.net> wrote in
news:Nr******** ********@newsre ad1.dllstx09.us .to.verio.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.go oglegroups.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_OVERFLO W | _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.lan guage' awaits you.

V
Jul 22 '05 #10

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

Similar topics

4
3301
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 interact with a rich 3D environment. Seem to be 90% of the way there! My problems are related to calculations where the result tends to zero (or another defined limit.) Have loads of cases where this kind of interaction occurs but this one
5
3734
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 session we found out, that our program was working correctly, but the floating point processing...
687
23349
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 believe that. in pieces of the application where speed really matters you can still use "normal" functions or even static methods which is basically the same. in C there arent the simplest things present like constants, each struct and enum...
3
3792
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 programmers who don't range- check their data can e.g. multiply two very tiny values and end up with a very large one. This is (AFAICT) quite fine according to the Standard but is certainly a QoI issue and I'd like to raise ours. I believe that it's...
15
3923
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 the EPSILON? I know in float.h a FLT_EPSILON is defined to be 10^-5. Does this mean that the computer cannot distinguish between 2 numbers that differ by less than this epsilon?
9
1775
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 produce values like 0.10000000000000003, when what I really want is 0.1. But is there any way to eliminate that digit at the end? I've tried rounding, but that simply moves the digit to the least significant position, such as 0.1000003. Failing...
33
2758
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
3552
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 out there understands what happens. Essentially, when I subtract the (double) function value GRID_POINT(2) from a variable which has been assigned the same value before this gives a non-zero result and I really do not understand why.
5
3068
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 floating point numbers precisely, but I was under the impression that the standard used for that last digit would prevent subtraction errors from compounding. Is there a simple solution to this problem, or do I need to run some sort of check at...
0
8432
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8855
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6185
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
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
2
1743
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.