What kind of data (double, float) should I use when representing a temperature?

Asked

Viewed 1,126 times

9

My question is regarding the use of Double and of Float in Java exercises when having to declare a temperature (Celsius scale). Which one should I use in this case?

I declared as Double, but the teacher when correcting the exercise in class ended up using the Float.

I know that between these two data the difference is the accuracy they present, but what I still can’t understand is when each of them, and for what purposes the Double and the Float are used.

  • 1

    you can find a great explanation here.

  • thank you Gabriel!

  • 3

    I think it’s worth asking the teacher why float is "wrong" in this case. At first, temperatures do not require such great precision that a float cannot withstand. Unless there is some other detail in the exercise that "forces" you to use double (or makes it the best choice). But it looks like you’re using the Wrappers (Double and Float, uppercase), it may have to do with the answer. If you want [Edit] the question putting more details, such as the code snippet for example, who knows we can give more details on this specific case

  • 1

    And after asking the teacher, comment here what she said, which is for us to see if it is true. Not doubting her, of course, but just to be on the "safe" side. I have seen a lot of teachers (from renowned college, including) who clearly only read theory, but never had to do anything "real", by the type of class given.

  • @hkotsubo I think you misunderstood because "the teacher when correcting the exercise in class ended up using the Float! "

  • 2

    @Yeah, what a distraction for me. Even so, any of the types would serve and it remains strange that one of them is "wrong" and needs to be "corrected". Unless there is some detail in the exercise that "forces" to use float.

  • 1

    @hkotsubo Nothing obliges, however there is justification to choose float as explained in the reply of Manieiro and Victor.

Show 2 more comments

2 answers

11


There is no specific type suitable, for this case some can be used.

In fact, if you’re going to create types to handle these temperatures and you’re going to use a sophisticated infrastructure to represent them, the internal type that holds the numbers doesn’t matter much. It could use some strategies and depends on what you expect to accomplish. It may even be that a simple float solve easily.

If you’re going to use a generic type to represent you can even use a int, but probably the easiest is to use a float which already has the decimal part. With a int would have to always make a division to take the decimal part, do accounts to suit the scale you are using, have to take various care and should not be worth it for basic use and for more sophisticated use I think the previous solution is better.

Nothing prevents you from using a double, but I find this kind of thing exaggerated. The float saves memory and has more than enough precision for something simple like this. Can not say that is wrong, but would not be the first choice.

The difference between them is just the same precision, which obviously makes the most accurate (8 bytes versus 4). Do not confuse with accuracy.

If you had the statement to use something that takes up less space, you might even think about other things, even more if you have exactitude requirements. It seems to me that none of this is required, then float is better in real use, but it can be argued that the double no harm but to occupy memory for no reason, which changes little in most cases.

In this case I doubt that the BigDecimal is necessary, but only you can answer if it must have accuracy beyond the precision that the float already gives. See more on What is the correct way to use the float, double and decimal types?.

10

Let me copy some of the stuff I posted in this answer:

The float and the double are implemented in accordance with the IEEE 754 standard, used by virtually all modern programming languages working with 32 or 64 bit floating point numbers.

The float is represented with 32 bits of this form (screenshot of wikipedia):

a

Note the first bit, it is the signal bit. If it is 0 it is a positive number, if it is 1 it is negative. So the value of float is as follows:

(to) -1^sign x (1 + fraction/2^23) x 2^(expoent - 127), if the expoent is different from 0 and 255.

(b) -1^sign x fraction/2^23 x 2^-126, if the expoent is equal to 0.

(c) fórmula +inf, if the expoent is equal to 255 and fraction is equal to 0.

(d) Nan, if the expoent is equal to 255 and fraction other than 0.

The values of equation (a) are those that are called normal floating point numbers, while those of equation (b) are called subnormal or denormal floating point numbers. The values of (c) are those of infinity and (d) are Nan (not-a-number).

We still have the double that uses a similar concept, but with more different bits and values:

b

The equations of double are those:

(to) -1^sign x (1 + fraction/2^52) x 2^(expoent - 1023), if the expoent is different from 0 and 2047.

(b) -1^sign x fraction/2^52 x 2^-1022, if the expoent is equal to 0.

(c) fórmula +inf, if the expoent is equal to 2047 and fraction is equal to 0.

(d) Nan, if the expoent is equal to 2047 and fraction other than 0.

Well, the double has a higher storage capacity than the float both in the exponent and in the mantissa (fraction). So, all the value float may be represented by a double, but the opposite is not true. Therefore, the question to be asked is whether the float offers sufficient accuracy.

The float has 23 bits to represent the fraction. This is enough to store whole numbers while maintaining the expoent 0 in the range 0 to 224 - 1 = 16777215. To represent 16777217, it loses the least significant bit 1 by rounding to 16777216.

This means that numbers with 0.5 accuracy can be stored up to 8388607.5. With 0.25 accuracy, it is up to 4194303.75. Maintaining a sufficient accuracy to represent 3 boxes after the decimal point (with 1/1024 accuracy), it is possible to represent numbers up to 16383,999.

Representing numbers that represent temperatures with precision of thousandths of degrees up to a temperature of more than 16000 degrees, shows that the float is accurate enough for this. So both the float as to the double can be used.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.