473,699 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inconsistent double precision math


I'm seeing a very strange behavior with double precision subtraction.
I'm using csUnit for testing. If I run the test by itself, the test
passes. When I run the batch of tests, the test fails. Here's the
test:

[Test]
public void GetAcuteAngleDi fference_PI()
{
double a = 0;
double b = Math.PI;

Assert.Equals( b, Euclid.GetAcute AngleDifference (a, b));
}
/// <summary>
/// Get the smallest acute difference between 2 angles. A positive
result
/// shows that b is clockwise of a, while a negative shows that b is
/// counter clockwise of a.
/// </summary>
/// <param name="a">angle in radians</param>
/// <param name="b">angle in radians</param>
/// <returns>smalle st accute angle in radians</returns>
public static double GetAcuteAngleDi fference(double a, double b)
{
a = SnapAngle(a); //place the angle between 0 and 2 Pi
b = SnapAngle(b);

if (a == b) return 0;

double result = b-a; //////////This line gives me grief.///////

double absResult = Math.Abs(result );
double direction = absResult / result;

if (absResult > Math.PI) //the acute angle is the opposite
direction
result = direction * (absResult - (2 * Math.PI));

return result;
}

The test is looking for the smallest acute angle between 0 and Pi. The
expected result is Pi.

Like I said, if I run the test by iteself, it passes. The 'result'
after the (b-a) subtraction is essentially (Math.PI - 0) and the result
the same as Math.PI = 3.141592 6535897931. However, the batch run
shows 'result' variable is slightly bigger at 3.141592 7410125732.

Has anyone else experienced this behavior? What can I do to correct it
or work around it?

BTW, the test is running a Debug build on a 2.8 Ghz Pentium 4.

Thanks!

Marshall

Nov 17 '05 #1
2 5877
Hi Marshall,
I am not sure why your test returns different results when run
individually or as part of a batch. As we know floating point numbers cannot
be precise in some cases as they cannot represent all possible contiguous
numbers, I would add an epsilon value (a small error margin) to the
Assert.Equals function, it is possible as one of the overloaded parameters,
which would allow your test to fail even if the numbers are not perfectly
identical, but within accepted error margins.

Mark R Dawson

"mbelew" wrote:

I'm seeing a very strange behavior with double precision subtraction.
I'm using csUnit for testing. If I run the test by itself, the test
passes. When I run the batch of tests, the test fails. Here's the
test:

[Test]
public void GetAcuteAngleDi fference_PI()
{
double a = 0;
double b = Math.PI;

Assert.Equals( b, Euclid.GetAcute AngleDifference (a, b));
}
/// <summary>
/// Get the smallest acute difference between 2 angles. A positive
result
/// shows that b is clockwise of a, while a negative shows that b is
/// counter clockwise of a.
/// </summary>
/// <param name="a">angle in radians</param>
/// <param name="b">angle in radians</param>
/// <returns>smalle st accute angle in radians</returns>
public static double GetAcuteAngleDi fference(double a, double b)
{
a = SnapAngle(a); //place the angle between 0 and 2 Pi
b = SnapAngle(b);

if (a == b) return 0;

double result = b-a; //////////This line gives me grief.///////

double absResult = Math.Abs(result );
double direction = absResult / result;

if (absResult > Math.PI) //the acute angle is the opposite
direction
result = direction * (absResult - (2 * Math.PI));

return result;
}

The test is looking for the smallest acute angle between 0 and Pi. The
expected result is Pi.

Like I said, if I run the test by iteself, it passes. The 'result'
after the (b-a) subtraction is essentially (Math.PI - 0) and the result
the same as Math.PI = 3.141592 6535897931. However, the batch run
shows 'result' variable is slightly bigger at 3.141592 7410125732.

Has anyone else experienced this behavior? What can I do to correct it
or work around it?

BTW, the test is running a Debug build on a 2.8 Ghz Pentium 4.

Thanks!

Marshall

Nov 17 '05 #2
Hi,

Take a look at Jon Skeet's article about flaoting points number

:http://www.yoda.arachsys.com/csharp/floatingpoint.html
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"mbelew" <mb****@koioswo rks.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .

I'm seeing a very strange behavior with double precision subtraction.
I'm using csUnit for testing. If I run the test by itself, the test
passes. When I run the batch of tests, the test fails. Here's the
test:

[Test]
public void GetAcuteAngleDi fference_PI()
{
double a = 0;
double b = Math.PI;

Assert.Equals( b, Euclid.GetAcute AngleDifference (a, b));
}
/// <summary>
/// Get the smallest acute difference between 2 angles. A positive
result
/// shows that b is clockwise of a, while a negative shows that b is
/// counter clockwise of a.
/// </summary>
/// <param name="a">angle in radians</param>
/// <param name="b">angle in radians</param>
/// <returns>smalle st accute angle in radians</returns>
public static double GetAcuteAngleDi fference(double a, double b)
{
a = SnapAngle(a); //place the angle between 0 and 2 Pi
b = SnapAngle(b);

if (a == b) return 0;

double result = b-a; //////////This line gives me grief.///////

double absResult = Math.Abs(result );
double direction = absResult / result;

if (absResult > Math.PI) //the acute angle is the opposite
direction
result = direction * (absResult - (2 * Math.PI));

return result;
}

The test is looking for the smallest acute angle between 0 and Pi. The
expected result is Pi.

Like I said, if I run the test by iteself, it passes. The 'result'
after the (b-a) subtraction is essentially (Math.PI - 0) and the result
the same as Math.PI = 3.141592 6535897931. However, the batch run
shows 'result' variable is slightly bigger at 3.141592 7410125732.

Has anyone else experienced this behavior? What can I do to correct it
or work around it?

BTW, the test is running a Debug build on a 2.8 Ghz Pentium 4.

Thanks!

Marshall

Nov 17 '05 #3

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

Similar topics

8
1813
by: Michel | last post by:
Hi there, I need to make a poisson distribution function that uses: double Math.Exp(double d) The d argument is a negative number in my case. When d becomes bigger and bigger, the result loses precision and is finally 0 (when d reaches 650) because it is too small to fit in a double. In my case, d can become -10000, so I need to have a greater precision number and Exp function.
5
6085
by: DAVID SCHULMAN | last post by:
I've been trying to perform a calculation that has been running into an underflow (insufficient precision) problem in Microsoft Excel, which calculates using at most 15 significant digits. For this purpose, that isn't enough. I was reading a book about some of the financial scandals of the 1990s called "Inventing Money: The Story of Long-Term Capital Management and the Legends Behind it" by Nicholas Dunbar. On page 95, he mentions that...
6
7269
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my tests confirm this. However, most of the Math methods deal with doubles and I'm having problems with casting them back to floats. For example: (double)0.1f = 0.10000000149011612 I don't need the accuracy of doubles, but the speed of floats would be benificial, but it seems I'm either...
4
409
by: mbelew | last post by:
I'm seeing a very strange behavior with double precision subtraction. I'm using csUnit for testing. If I run the test by itself, the test passes. When I run the batch of tests, the test fails. Here's the test: public void GetAcuteAngleDifference_PI() { double a = 0; double b = Math.PI;
67
9895
by: lcw1964 | last post by:
This may be in the category of bush-league rudimentary, but I am quite perplexed on this and diligent Googling has not provided me with a clear straight answer--perhaps I don't know how to ask the quesion. I have begun to familiarize myself here with the gcc compiler in a win32 environment, in the form of MinGW using both Dev C++ and MSYS as interfaces. I have recompiled some old math code that uses long double types throughout and...
9
8275
by: richard_lavoie | last post by:
Hi, I have something like this: vector<floatvec1; and I want to cast it, so I use vector vec2<double= static_cast< vector<double(vec1); I always become a error: syntax error before `>' token in that line
1
3228
by: bachyen | last post by:
I have the following code, but it is error when I build it. Can you help me? Can you tell me more about 'Inconsistent accessibility: return type', 'Inconsistent accessibility: parameter type'. using System; public class Tinh_khoang_cach_Ham_tu_dinh_nghia { struct DIEM { public Double x;
29
2959
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any bug , i am still c++ beginner
13
3141
by: =?Utf-8?B?U3RlZmFuRw==?= | last post by:
With the test I've made on my WinXP/VS2005/C#2.0, I get a precision of 42bit. Is this specified in C# or dependent on what? double dLeft = 2183.23 - 695.37; double dRight = 1487.86; double dFraction = Math.Pow(2, -42); if (Math.Abs(dLeft - dRight) == dFraction) { System.Console.WriteLine("Success"); } // Output: Success
0
8704
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
8623
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
9054
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8895
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6545
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
5879
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();...
1
3071
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
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
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.