EzDevInfo.com

double interview questions

Top double frequently asked interview questions

How do you test to see if a double is equal to NaN?

I have a double in Java and I want to check if it is NaN. What is the best way to do this?


Source: (StackOverflow)

Round a double to 2 decimal places [duplicate]

This question already has an answer here:

If the value is 200.3456, it should be formatted to 200.34. If it is 200, then it should be 200.00.


Source: (StackOverflow)

Advertisements

Checking if a double (or float) is NaN in C++

Is there an isnan() function?

PS.: I'm in MinGW (if that makes a difference).

I had this solved by using isnan() from <math.h>, which doesn't exist in <cmath>, which I was #includeing at first.


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)

Moving decimal places over in a double

So I have a double set to equal 1234, I want to move a decimal place over to make it 12.34

So to do this I multiply .1 to 1234 two times, kinda like this

double x = 1234;
for(int i=1;i<=2;i++)
{
  x = x*.1;
}
System.out.println(x);

This will print the result, "12.340000000000002"

Is there a way, without simply formatting it to two decimal places, to have the double store 12.34 correctly?


Source: (StackOverflow)

Retain precision with double in Java

public class doublePrecision {
    public static void main(String[] args) {

        double total = 0;
        total += 5.6;
        total += 5.8;
        System.out.println(total);
    }
}

The above code prints:

11.399999999999

How would I get this to just print (or be able to use it as) 11.4?


Source: (StackOverflow)

How to print double value without scientific notation using Java?

I want to print a double value in Java without exponential form.

double dexp = 12345678;
System.out.println("dexp: "+dexp);

It shows this E notation: 1.2345678E7.

I want it to print it like this: 12345678

What is the best way to prevent this?


Source: (StackOverflow)

0.1 float is greater than 0.1 double. I expected it to be false [duplicate]

Let:

double d = 0.1;
float f = 0.1;

should the expression

(f > d)

return true or false?

Empirically, the answer is true. However, I expected it to be false.

As 0.1 cannot be perfectly represented in binary, while double has 15 to 16 decimal digits of precision, and float has only 7. So, they both are less than 0.1, while the double is more close to 0.1.

I need an exact explanation for the true.


Source: (StackOverflow)

How many double numbers are there between 0.0 and 1.0?

This is something that's been on my mind for years, but I never took the time to ask before.

Many (pseudo) random number generators generate a random number between 0.0 and 1.0. Mathematically there are infinite numbers in this range, but double is a floating point number, and therefore has a finite precision.

So the questions are:

  1. Just how many double numbers are there between 0.0 and 1.0?
  2. Are there just as many numbers between 1 and 2? Between 100 and 101? Between 10^100 and 10^100+1?

Note: if it makes a difference, I'm interested in Java's definition of double in particular.


Source: (StackOverflow)

Why is a round-trip conversion via a string not safe for a double?

Recently I have had to serialize a double into text, and then get it back. The value seems to not be equivalent:

double d1 = 0.84551240822557006;
string s = d1.ToString("R");
double d2 = double.Parse(s);
bool s1 = d1 == d2;
// -> s1 is False

But according to MSDN: Standard Numeric Format Strings, the "R" option is supposed to guarantee round-trip safety.

Why did this happen?


Source: (StackOverflow)

How do I parse a string with a decimal point to a double?

I want to parse a string like "3.5" to a double. However,

double.Parse("3.5") 

yields 35 and

double.Parse("3.5", System.Globalization.NumberStyles.AllowDecimalPoint) 

throws a FormatException.

Now my computer's locale is set to German, wherein a comma is used as decimal separator. It might have to do something with that and double.Parse() expecting "3,5" as input, but I'm not sure.

How can I parse a string containing a decimal number that may or may not be formatted as specified in my current locale?


Source: (StackOverflow)

Is it possible to get 0 by subtracting two unequal floating point numbers?

Is it possible to get division by 0 (or infinity) in the following example?

public double calculation(double a, double b)
{
     if (a == b)
     {
         return 0;
     }
     else
     {
         return 2 / (a - b);
     }
}

In normal cases it will not, of course. But what if a and b are very close, can (a-b) result in being 0 due to precision of the calculation?

Note that this question is for Java, but I think it will apply to most programming languages.


Source: (StackOverflow)

Convert String to double in Java

I want to change my double value to a String.

if((e.getSource() == jBook)) {
    String name = jlbName.getText();
    String date = jlbDateProduce.getText();
    String time = jr1.getText();
    int number = (Integer.parseInt(jtfNoOfTicket.getText().trim()));
    String total = jlbTotal.getText();
    String price = jlbPrice.getText();

    //Passing
    ticketReservation frame = new ticketReservation(
        name, date, time, price, total, String.valueOf(number));
}

In another class, I need to use that value to count the total. Is there any way to convert String back to double?


Source: (StackOverflow)

Why does this random value have a 25/75 distribution instead of 50/50?

Edit: So basically what I'm trying to write is a 1 bit hash for double.

I want to map a double to true or false with a 50/50 chance. For that I wrote code that picks some random numbers (just as an example, I want to use this on data with regularities and still get a 50/50 result), checks their last bit and increments y if it is 1, or n if it is 0.

However, this code constantly results in 25% y and 75% n. Why is it not 50/50? And why such a weird, but straight-forward (1/3) distribution?

public class DoubleToBoolean {
    @Test
    public void test() {

        int y = 0;
        int n = 0;
        Random r = new Random();
        for (int i = 0; i < 1000000; i++) {
            double randomValue = r.nextDouble();
            long lastBit = Double.doubleToLongBits(randomValue) & 1;
            if (lastBit == 1) {
                y++;
            } else {
                n++;
            }
        }
        System.out.println(y + " " + n);
    }
}

Example output:

250167 749833

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)