floating-point

Can I expect float variable values that I set from literal constants to be unchanged after assignment to other variables?

If I do something like this: float a = 1.5f; float b = a; void func(float arg) { if (arg == 1.5f) printf("You are teh awresome!"); } func(b); Will the text print every time (and on every machine)? EDIT I mean, I'm not really sure if the value will pass through the FPU at some point even if I'm not doing any calculations, and if ...

Elegant workaround for JavaScript floating point number problem

Hi, I have the following dummy test script: function test(){ var x = 0.1 * 0.2; document.write(x); } test(); This will print the result "0.020000000000000004" while it should just print "0.02" (if you use your calculator. As far as I understood this is due to errors in the floating point multiplication precision. Does anyone...

Platform-independent way to obtain maximum C++ float value

What’s the best, platform-independent way to obtain the maximum value that can be stored in a float in C++? ...

Why is there a difference between the same value stored as a float and a double in Java?

I expected the following code to produce: "Both are equal", but I got "Both are NOT equal": float a=1.3f; double b=1.3; if(a==b) { System.out.println("Both are equal"); } else{ System.out.println("Both are NOT equal"); } What is the reason for this? ...

Converting from a text field to an numeric in Android

If I have an EditText component on my screen that I have specified inputType="decimal" for (i.e. a numeric/decimal field), what is the best way to convert it to an decimal value in the application code? Google recommends avoiding floats, and avoiding creating objects unnecessarily (and I assume any auto-unboxing code is bad too), so I...

Why is x and y always 0 with a float?

int main(void){ float x =1; float y =2; while (x<y){ x = x +1 ; y = y +1; } printf("x is %d\n", x); printf("y is %d\n", y); } I would expect x and y to increase to the point we run out of bits, but it seems like x and y is alway 0 in this case... ...

C++ version of isnormal()

Is there a C++ version of the isnormal, isnan and so C functions? I know I can use the C functions from C++, but I'm always interested to know if there are some C++-only alternatives. ...

Check double variable if it contains an integer, and not floating point

What I mean is the following: double d1 =555; double d2=55.343 I want to be able to tell that d1 is an integer while d2 is not. Is there an easy way to do it in c/c++? ...

What is the most efficient way to store and work with a floating point number with 1,000,000 significant digits in C?

I'm writing a utility to calculate π to a million digits after the decimal. On a 32- or 64-bit consumer desktop system, what is the most efficient way to store and work with such a large number accurate to the millionth digit? clarification: The language would be C. ...

Floating point stack handling with floating-point exceptions turned on

I'm running into an issue with floating point exceptions turned on in Visual Studio 2005. If I have code like this: double d = 0.0; double d2 = 3.0; double d3 = d2/d; and if I register an SEH handler routine, then I can easily turn the div-by-zero into a C++ exception and catch it. So far so good. However, when I do this, the f...

Comparing floating point values

I just read a statement about the floating point value comparison Floating point values shall not be compared using either the == or != operators. Most floating point values have no exact binary representation and have a limited precision. If so what is the best method for comparing two floating point values? ...

How to divide two doubles accurately

I have two double a, b; I know that the following is true -1 <= a/b <= 1 however b can be arbitrarily small. When I do this naively and just compute the value a/b the condition specified above does not hold in some cases and I get values like much greater than 1 in absolute value (like 13 or 14.) How can I ensure that when ...

How to handle big integers (64 bit) numbers in tcl?

what is the best way to handle big integers in expr command. we know wide in expr. ...

Double to string conversion without scientific notation

How to convert a double into a floating-point string representation without scientific notation in the .NET Framework? "Small" samples (effective numbers may be of any size, such as 1.5E200 or 1e-200) : 3248971234698200000000000000000000000000000000 0.00000000000000000000000000000000000023897356978234562 None of the standard number...

C# Nth Root of small number precision problem

When I try to take the N th root of a small number using C# I get a wrong number. For example when I try to take the 3rd root of 1.07, I get 1, which is clearly not true. Here is the exact code I am using to get the 3rd root. MessageBox.Show(Math.Pow(1.07,(1/3)).toString()); Can anyone tell me how to solve this problem. I would gue...

Is there a Java equivalent of frexp?

Is there a Java equivalent of the C / C++ function called frexp? If you aren't familiar, frexp is defined by Wikipedia to "break floating-point number down into mantissa and exponent." I am looking for an implementation with both speed and accuracy but I would rather have the accuracy if I could only choose one. This is the code sampl...

Haskell: Force floats to have two decimals

Using the following code snippet: (fromIntegral 100)/10.00 Using the Haskell '98 standard prelude, how do I represent the result with two decimals? Thanks. ...

What is the rationale for all comparisons returning false for IEEE754 NaN values?

Why do comparisons of NaN values behave differently from all other values? That is, all comparisons with the operators ==, <=, >=, <, > where one or both values is NaN returns false, contrary to the behaviour of all other values. I suppose this simplifies numerical computations in some way, but I couldn't find an explicitly stated reaso...

iPhone and floating point math

Hi, I have following code: float totalSpent; int intBudget; float moneyLeft; totalSpent += Amount; moneyLeft = intBudget - totalSpent; And this is how it looks in debugger: http://www.braginski.com/math.tiff Why would moneyLeft calculated by the code above is .02 different compared to the expression calculated by the ...

Why do simple math operations on floating point return unexpected (inacurate) results in VB.Net and Python?

x = 4.2 - 0.1 vb.net gives 4.1000000000000005 python gives 4.1000000000000005 Excel gives 4.1 Google calc gives 4.1 What is the reason this happens? ...