EzDevInfo.com

floating-point interview questions

Top floating-point frequently asked interview questions

Why not use Double or Float to represent currency?

I've always been told never to represent money with double or float types, and this time I pose the question to you: why?

I'm sure there is a very good reason, I simply do not know what it is.


Source: (StackOverflow)

Difference between Decimal, Float and Double in .NET?

What is the difference between Decimal, Float and Double in .NET?

When would someone use one of these?


Source: (StackOverflow)

Advertisements

Is floating point math broken?

0.1 + 0.2 == 0.3
-> false
0.1 + 0.2
-> 0.30000000000000004

Any ideas why this happens?


Source: (StackOverflow)

Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?

I am doing some numerical optimization on a scientific application. One thing I noticed is that GCC will optimize the call pow(a,2) by compiling it into a*a, but the call pow(a,6) is not optimized and will actually call the library function pow, which greatly slows down the performance. (In contrast, Intel C++ Compiler, executable icc, will eliminate the library call for pow(a,6).)

What I am curious about is that when I replaced pow(a,6) with a*a*a*a*a*a using GCC 4.5.1 and options "-O3 -lm -funroll-loops -msse4", it uses 5 mulsd instructions:

movapd  %xmm14, %xmm13
mulsd   %xmm14, %xmm13
mulsd   %xmm14, %xmm13
mulsd   %xmm14, %xmm13
mulsd   %xmm14, %xmm13
mulsd   %xmm14, %xmm13

while if I write (a*a*a)*(a*a*a), it will produce

movapd  %xmm14, %xmm13
mulsd   %xmm14, %xmm13
mulsd   %xmm14, %xmm13
mulsd   %xmm13, %xmm13

which reduces the number of multiply instructions to 3. icc has similar behavior.

Why do compilers not recognize this optimization trick?


Source: (StackOverflow)

How do I check if a string is a number (float) in Python?

What is the best possible way to check if a string can be represented as a number in Python?

The function I currently have right now is:

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

Which, not only is ugly and slow, seems clunky. However I haven't found a better method because calling float in the main function is even worse.


Source: (StackOverflow)

Limiting floats to two decimal points

I want a to be rounded to 13.95.

>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999

The round function does not work the way I expected.


Source: (StackOverflow)

When should I use double instead of decimal?

I can name three advantages to using double (or float) instead of decimal:

  1. Uses less memory.
  2. Faster because floating point math operations are natively supported by processors.
  3. Can represent a larger range of numbers.

But these advantages seem to apply only to calculation intensive operations, such as those found in modeling software. Of course, doubles should not be used when precision is required, such as financial calculations. So are there any practical reasons to ever choose double (or float) instead of decimal in "normal" applications?

Edited to add: Thanks for all the great responses, I learned from them.

One further question: A few people made the point that doubles can more precisely represent real numbers. When declared I would think that they usually more accurately represent them as well. But is it a true statement that the accuracy may decrease (sometimes significantly) when floating point operations are performed?


Source: (StackOverflow)

When should I use the "strictfp" keyword in java?

Ok, I've looked up what this does, but does anyone actually have an example of when you would use the "strictfp" keyword in java? Has anyone actually found a use for this?

Would there be any side-effects of just putting it on all my floating point operations?


Source: (StackOverflow)

How to convert float to int with Java

I used the following line to convert float to int, but it's not as accurate as I'd like:

 float a=8.61f;
 int b;

 b=(int)a;

The result is : 8 (It should be 9)

When a = -7.65f, the result is : -7 (It should be -8)

What's the best way to do it ?


Source: (StackOverflow)

How can I force division to be floating point in Python?

I have two integer values a and b, but I need their ratio in floating point. I know that a<b and I want to calculate a/b, so if I use integer division I'll always get 0 with a remainder of a.

How can I force c to be a floating point number in Python in the following?

c = a / b

Source: (StackOverflow)

Parse String to Float or Int

In Python, how can I parse a numeric string like "545.2222" to its corresponding float value, 542.2222? Or parse the string "31" to an integer, 31?

I just want to know how to parse a float string to a float, and (separately) an int string to an int.


Source: (StackOverflow)

round() for float in C++

I need a simple floating point rounding function, thus:

double round(double);

round(0.1) = 0
round(-0.1) = 0
round(-0.9) = -1

I can find ceil() and floor() in the math.h - but not round().

Is it present in the standard C++ library under another name, or is it missing??


Source: (StackOverflow)

How do I print a double value with full precision using cout?

So I've gotten the answer to my last question (I don't know why I didn't think of that). I was printing a double using cout that got rounded when I wasn't expecting it. How can I make cout print a double using full precision?


Source: (StackOverflow)

Difference between float and double

I know, I've read about the difference between double precision and single precision, etc. But they should give the same results on most cases right?

I was solving a problem on a programming contest and there were calculations with floating point numbers that were not really big, so I decided to use float instead of double, and I checked it - I was getting the correct results. But when I send the solution, it said only 1 of 10 tests was correct. I checked again and again, until I found that using float is not the same using double. I put double for the calculations and double for the output, and the program gave the SAME results, but this time it passed all the 10 tests correctly.

I repeat, the output was the SAME, the results were the SAME, but putting float didn't work - only double. The values were not so big too, and the program gave the same results on the same tests both with float and double, but the online judge accepted only the double-provided solution.

Why? What is the difference?


Source: (StackOverflow)

Format number to always show 2 decimal places

I would like to format my numbers to always display 2 decimal places, rounding where applicable.

Examples:

number     display
------     -------
1          1.00
1.341      1.34
1.345      1.35

I have been using this:

parseFloat(num).toFixed(2);

But it's displaying 1 as 1, rather than 1.00.


Source: (StackOverflow)