References for invalid floating point operation

Source: Internet
Author: User

Tripping floating-point exceptions

Floating-point limits tions are, as you might expect CT by reason ue of the name, rare. the ones that happen most commonly by mistake in my experience are the zero-divide and invalid operation exceptions. zero divide tends to happen whenever you have an unchecked normalization operation, such as resetting a 2D or 3D vector to unit length-which works fine, until someone hands you a vector of Length zero. another example wocould be trying to normalize a portion of audio that was totally silent. when the zero-divide exception is masked, the FPU spits out a signed infinity instead, which sometimes works out in the end. for instance, if the expression is of the form | x/y |> N, then the Infinity wowould give you the correct result.

Invalid operation exceptions are more serous and result from operations that don't have a graceful way to degrade, such as 0/0, the square root of-1, etc. these too often result from the lack of bounds checks. for instance, a common way to determine the angle between two vectors is through dot product, since the shortest angle between two vectors is ACOs (dot (V1/| V1 |, v2/| V2 | )). unfortunately, the common way of normalizing vectors is to multiply by the reciprocal square root of the squared length (dot (V, V )), which can give you a not-quite-unit-length vector since the squaring operation discards half of the usual precision. this can then lead to taking the arccosine of a number slightly larger than 1. when such an operation occurs and invalid operation tions are masked, the FPU spits out a not a number (NAN) value and keeps going. you can also trip such an exception by trying to operate on nans, especially by loading garbage data that isn' t a valid IEEE finite number.

In general, you don't want to be tripping floating-point conditions, even if they are masked. the reason is that when the FPU hits one, the fast hardware can't handle it and punts to the microcode, which then takes about twenty times longer. this is especially bad with nans since any operation with a nan produces another Nan, causing them to spread throughout your calculations (Nan disease) and slow down everything massively. you can even crash due to nans blowing past clamp expressions, since any comparison with a nan is false and converting one to integer form results in integer indefinite (0x80000000 ). despite the erroneous results, though, nans can appear sporadically in a large Win32 program without anyone knowing, and may go unnoticed in a code base for years.

Note that although exceptions are really slow and usually indicate mistakes, the results when the specified tions are masked are well-defined. it is possible, and sometimes reasonable, to actually depend on and test for specific results from Masked exceptions. so it isn't valid to simply say "Don't do that."

Solution:

 
Turning off floating point exceptions in BCB and Delphi

When running floating point code, such as that found in Direct 3D, you will often get a series of floating point exceptions. microsoft supporesses these exceptions by default, but we raise them. it is easy to turn this option off in both C ++ builder and Delphi.

Here is how to turn them off in C ++ builder:

#include float.h__fastcall TForm1::TForm1(TComponent* Owner)    : TForm(Owner){  _control87(MCW_EM, MCW_EM);}

Here is how to turn them off in Delphi:

  const    MCW_EM = DWord($133f);  begin    Set8087CW(MCW_EM);  end;
Generally, you need to check the upper and lower limits of the array so that it does not exceed the range.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.