rounding-error

How is floating point stored? When does it matter?

In follow up to this question, it appears that some numbers cannot be represented by floating point at all, and instead are approximated. How are floating point numbers stored? Is there a common standard for the different sizes? What kind of gotchas do I need to watch out for if I use floating point? Are they cross-language compatibl...

How do I cope with rounding errors on doubles in vb.net?

I'm trying to balance a set of currency values using vb.net. The totals for both these values is cast as a double. I'm getting rounding errors in some situations. What's the best way to avoid this? Is there a type I can use in preference to double? How do I round the resultant value to two decimal places? Here's my code - I probably d...

How to find mantissa length on a particular machine?

I'm wanting to find the number of mantissa digits and the unit round-off on a particular computer. I have an understanding of what these are, just no idea how to find them - though I understand they can vary from computer to computer. I need this number in order to perform certain aspects of numerical analysis, like analyzing errors. ...

How to handle multiplication of numbers close to 1

I have a bunch of floating point numbers (Java doubles), most of which are very close to 1, and I need to multiply them together as part of a larger calculation. I need to do this a lot. The problem is that while Java doubles have no problem with a number like: 0.0000000000000000000000000000000001 (1.0E-34) they can't represent some...

php intval() and floor() return value that is too low?

Because the float data type in PHP is inaccurate, and a FLOAT in MySQL takes up more space than an INT (and is inaccurate), I always store prices as INTs, multipling by 100 before storing to ensure we have exactly 2 decimal places of precision. However I believe PHP is misbehaving. Example code: echo "<pre>"; $price = "1.15"; echo "Pri...

What class to use for money representation?

What class should I use for representation of money to avoid most rounding errors? Should I use Decimal, or a simple built-in number? Is there any existing Money class with support for currency conversion that I could use? Any pitfalls that I should avoid? ...

Difference in rounding in sql and clr

I have searched and can not find the answer. I double checked the data types between SQL and CLR and I appear to have that correct. But I am getting a different result between using CLR and SQL. Not much, but enough to be off a penny. And that is not acceptable. Example in VB.NET Dim dLoanAmount As Decimal = 169500 Dim dNetDisc...

Specifying DPI of a GDI Device Context

I have an application that generates metafiles (EMFs). It uses the reference device (aka the screen) to render these metafiles, so the DPI of the metafile changes depending on what machine the code is running on. Let's say my code is intending to create a metafile that is 8.5 in x 11 in. Using my development workstation as a reference, ...

Representing probability in C++

I'm trying to represent a simple set of 3 probabilities in C++. For example: a = 0.1 b = 0.2 c = 0.7 (As far as I know probabilities must add up to 1) My problem is that when I try to represent 0.7 in C++ as a float I end up with 0.69999999, which won't help when I am doing my calculations later. The same for 0.8, 0.80000001. Is...

.NET rounding error in ToString("f2")

Hello I have this code in C#: float n = 2.99499989f; MessageBox.Show("n = " + n.ToString("f2", CultureInfo.InvariantCulture)); And this code in C++: float n = 2.99499989f; printf("n = %.2f", n); First one outputs 3.00. Second one outputs 2.99. I have no clue why this is happening. Update: I also tried Objective-C NSLog and the o...

SQL Rounding Problems in 2005 and 2000

I have a value in the database which is 2.700000002. When I run a query in Management studio in SQL SERVER 2005 I get 2.7. But when I run in SQL SERVER 2000 query analyzer it comes 2.700000002. 2.70000002 is correct why is SQL SERVER 2005 trying to change the value by rounding it or selecting the floor value? ...

Why does 99.99 / 100 = 0.9998999999999999

Possible Duplicate: Dealing with accuracy problems in floating-point numbers Whereas 99.99 * 0.01 = 0.99 Clearly this is the age old floating point rounding issue, however the rounding error in this case seems quite large to me; what I mean is I might have expected a result of 0.99990000001 or some similar 'close' result. An...

Estimating error on calculations using decimals

We're currently using System.Decimals to represent numbers in a .NET application we're developing. I know that decimals are design to minimize errors due to rounding, but I also know that certain numbers, 1/3 for example, cannot be represented as a decimal so some calculations will have small rounding error. I believe the magnitude of th...

Detecting precision loss when converting from double to float

I am writing a piece of code in which i have to convert from double to float values. I am using boost::numeric_cast to do this conversion which will alert me of any overflow/underflow. However i am also interested in knowing if that conversion resulted in some precision loss or not. For example double source = 1988.1012; floa...

Why is this Sql Server CAST(..) rounding a Decimal to a VarChar ?

Hi folks, I'm trying convert some decimals to varchar, but they are getting rounded. Can someone tell me why? declare @UpperLeftLatitude DECIMAL, @UpperLeftLongitude DECIMAL, @BottomRightLatitude DECIMAL, @BottomRightLongitude DECIMAL SET @UpperLeftLatitude = 38.663 SET @UpperLeftLongitude = -122.857 SET @BottomRightLatit...

Avoiding rounding in convert.tosingle method

consider , object a =1.123456; float f = convert.ToSingle(a); But when I print the value of f , I get 1.123455. It is getting rounded off. Also the problem is I cant change the data type of float in the code. Please help. ...

Minimizing the effect of rounding errors caused by repeated operations effectively.

I just recently came across the Kahan (or compensated) summation algorithm for minimizing roundoff, and I'd like to know if there are equivalent algorithms for division and/or multiplication, as well as subtraction (if there happens to be one, I know about associativity). Implementation examples in any language, pseudo-code or links woul...

Rounding Standards - Financial Calculations

I am curious about the existence of any "rounding" standards" when it comes to the calculation of financial data. My initial thoughts are to perform rounding only when the data is being presented to the user (presentation layer). If "rounded" data is then used for further calculations, should be use the "rounded" figure or the "raw" fig...