Temperature Variable in C++

Defining the variable.

Page 1 of 1

10 Replies - 1625 Views - Last Post: 31 October 2009 - 06:38 AM Rate Topic: -----

#1 JesusLover05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-October 09

Temperature Variable in C++

Posted 30 October 2009 - 08:20 PM

What is the best variable to use in C++ when programming temperature in fahrenheit or celsius? Would it be double or float?
Is This A Good Question/Topic? 0
  • +

Replies To: Temperature Variable in C++

#2 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: Temperature Variable in C++

Posted 30 October 2009 - 08:36 PM

You could probably just use int because neither of the temperatures need decimals to be fairly accurate. But if you must, float should suit.
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Temperature Variable in C++

Posted 30 October 2009 - 08:43 PM

In general, when you want floating point numbers, you want to use doubles. Unless you have a specific reason (and the technical understanding) to work with the far more limited precision of the float type, you'll want to stick to a double.
Was This Post Helpful? 0
  • +
  • -

#4 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: Temperature Variable in C++

Posted 30 October 2009 - 08:47 PM

View PostOler1s, on 30 Oct, 2009 - 07:43 PM, said:

In general, when you want floating point numbers, you want to use doubles. Unless you have a specific reason (and the technical understanding) to work with the far more limited precision of the float type, you'll want to stick to a double.


Say what??

The only difference between the 2 is the amount of decimal places each takes. Double takes more, but that also means more memory usage, and in temperature isn't necessary.

When has your weather forecast ever said: Today is cloud with a chance of rain. The current temperature is 50.028973787 F?

This post has been edited by Master Jake: 30 October 2009 - 08:48 PM

Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Temperature Variable in C++

Posted 30 October 2009 - 08:52 PM

Quote

The only difference between the 2 is the amount of decimal places each takes.
Yes, and this if of a significant concern. As a programmer, you should be aware of the problems in representing decimal numbers in binary floating point. The lack of precision can lead to various problems. There's no need to go out of the way with implementing arbitrary precision datatypes for every floating point, but in general, it's unlikely you want the trouble of even more limited precision for floating point numbers.

Quote

Double takes more, but that also means more memory usage, and in temperature isn't necessary.
So, you're telling me that when desktop computers normally have 1GB+ memory, you are worried about the memory difference of a float versus a double? Which do you think is more likely to be a problem: the memory, or the precision issues?

Quote

When has your weather forecast every said: Today is cloud with a chance of rain. The current temperature is 50.028973787 F?
So it's irrelevant in this example. That doesn't invalidate my rule of thumb.
Was This Post Helpful? 0
  • +
  • -

#6 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: Temperature Variable in C++

Posted 31 October 2009 - 01:45 AM

1. I was never arguing against double. I was only saying it's not needed in this situation.

2. I'm worried about everything. Yes, 4 bytes more of memory matters to me. Elegence and perfection is key. I don't always succeed, but I ALWAYS try.
Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Temperature Variable in C++

Posted 31 October 2009 - 02:24 AM

You don't need your straw man example of "50.028973787"
Two decimal places is enough to expose the flaws in float.

If I run this:
#include <iostream>
#include <string>
using namespace std;

int main()
{
	float floatTest;
	double doubleTest;
	
	floatTest  = 4.56/2;
	doubleTest = 4.56/2;
	
	if( floatTest == 2.28 ) {
		cout << "float good" << endl;
	}else{
		cout << "float bad" << endl;
	}
	
	if( doubleTest == 2.28 ) {
		cout << "double good" << endl;
	}else{
		cout << "double bad" << endl;
	}

    return 0;
}



I get this output
float bad
double good



Now tell me if the use of float in a program that is going to be doing even the simple maths that C to F (or the reverse) requires is "elegant" or just "buggy"?

If you want to be a good programmer here's a rule to remember: "Never prematurely optimize!".
Was This Post Helpful? 0
  • +
  • -

#8 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: Temperature Variable in C++

Posted 31 October 2009 - 02:32 AM

View Postjanotte, on 31 Oct, 2009 - 01:24 AM, said:

Now tell me if the use of float in a program that is going to be doing even the simple maths that C to F (or the reverse) requires is "elegant" or just "buggy"?


Temperature conversation generally ends in either a single decimal point (when using C to F) or a single decimal point repeating 30 times followed by another number. Float is fine for this situation.
Was This Post Helpful? 0
  • +
  • -

#9 stev3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 04-June 09

Re: Temperature Variable in C++

Posted 31 October 2009 - 04:05 AM

floats are 4 bytes (~7 digits), doubles are 8(~15)

but use an integer... you dont really want decimal points for the output of temperature :)
Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Temperature Variable in C++

Posted 31 October 2009 - 05:40 AM

View PostMaster Jake, on 31 Oct, 2009 - 01:32 AM, said:

Temperature conversation generally ends in either a single decimal point (when using C to F) or a single decimal point repeating 30 times followed by another number. Float is fine for this situation.


stev3, on 31 Oct, 2009 - 03:05 AM, said:

but use an integer... you dont really want decimal points for the output of temperature


Both of you are assuming you know what the Requirement Spec for the program is.
How do you know what precision is being requested by the customer?

The most interesting assumption is that truncation is acceptable.
If we arrive at 32.99 degrees as the 'correct' answer is the customer happy with 32 degrees as the answer (truncation via integer maths) or would they expect rounding up to 33 degrees in such a case?

I am not saying you are wrong. I am saying you are making assumptions that you are in no position to make about what the spec includes. I have no idea what is required here but I am 100% sure that no customer in the real world is going to be worried about whether you are or are not consuming 4 extra bytes if you are not providing the outcome they set out in their requirements.

Neither are your colleagues going to be happy with you leaving them time consuming maintenance tasks on code that has subtle bugs due to premature optimization.

Anyway, if you really think that optimizing for 4 bytes is more important than precision, robustness and maintainability then I'll leave you to that belief and hope you find it comforts and sustains you in your future programming endeavours.

This post has been edited by janotte: 31 October 2009 - 05:42 AM

Was This Post Helpful? 0
  • +
  • -

#11 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: Temperature Variable in C++

Posted 31 October 2009 - 06:38 AM

View Postjanotte, on 31 Oct, 2009 - 04:40 AM, said:

View PostMaster Jake, on 31 Oct, 2009 - 01:32 AM, said:

Temperature conversation generally ends in either a single decimal point (when using C to F) or a single decimal point repeating 30 times followed by another number. Float is fine for this situation.


stev3, on 31 Oct, 2009 - 03:05 AM, said:

but use an integer... you dont really want decimal points for the output of temperature


Both of you are assuming you know what the Requirement Spec for the program is.
How do you know what precision is being requested by the customer?

The most interesting assumption is that truncation is acceptable.
If we arrive at 32.99 degrees as the 'correct' answer is the customer happy with 32 degrees as the answer (truncation via integer maths) or would they expect rounding up to 33 degrees in such a case?

I am not saying you are wrong. I am saying you are making assumptions that you are in no position to make about what the spec includes. I have no idea what is required here but I am 100% sure that no customer in the real world is going to be worried about whether you are or are not consuming 4 extra bytes if you are not providing the outcome they set out in their requirements.

Neither are your colleagues going to be happy with you leaving them time consuming maintenance tasks on code that has subtle bugs due to premature optimization.

Anyway, if you really think that optimizing for 4 bytes is more important than precision, robustness and maintainability then I'll leave you to that belief and hope you find it comforts and sustains you in your future programming endeavours.


Every byte of memory adds up. I try to think about the bigger picture or the most plausible solution (whichever suits). In this case, I doubt a temperature conversion program is going to become large enough for the memory usage to really "matter" on today's machines; however, I still like to practice efficiency. Yes, I will sacrifice efficiency if it means a better program, so please don't assume things like that about me.

Also, I'd like to ask, where do you draw the line? OK, so float only takes 7 decimals and double takes 15. So you draw the line at 15? Why? Is it because that's your only other option. Numbers like PI go on, and on, and on, and on. When using PI you never get a fully accurate answer because it can iterate infinitely.

So what do you use for PI?

3.14
3.14159265

Use your double float example here. You're going to get different answers.

My math book tells me to use 3.14 because it is accepted as a decent round of PI; however, if I use the slightly longer version, the answer will differ by a few decimals.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1