LinuxQuestions.org Problems converting float to double in C
 Linux - Software This forum is for Software issues. Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

05-25-2009, 10:43 AM   #1
Ando140
LQ Newbie

Registered: Sep 2005
Posts: 9

Rep:
Problems converting float to double in C

I've been searching for days to figure out why I'm having this problem and I have yet to find a solution. From everything I've read, this shouldn't be a problem at all, but for some reason it doesn't seem to be working correctly. My debug code is posted below, dest->outTemp and dest->dewpoint are both defined as floats. Initially I was passing those straight into a function, but through my testing, my debug code has evolved into what you see below:

My Debug Code:
Quote:
 float CurTemp, CurDewpt; double DTemp, DDewpt; CurTemp = dest->outTemp; CurDewpt = dest->dewpoint; DTemp = CurTemp; DDewpt = CurDewpt; FILE *OutF; OutF = fopen("/root/wvBWvals.txt", "w"); fprintf(OutF, "temp= %f\n", dest->outTemp); fprintf(OutF, "dewpt= %f\n", dest->dewpoint); fprintf(OutF, "CurTemp= %f\n", CurTemp); fprintf(OutF, "CurDewpt= %f\n", CurDewpt); fprintf(OutF, "DTemp = %d\n", DTemp); fprintf(OutF, "DDewpt = %d\n", DDewpt); fclose(OutF);
Output:
Quote:
 temp= 88.339996 dewpt= 55.805176 CurTemp= 88.339996 CurDewpt= 55.805176 DTemp = -2147483648 DDewpt = 0
Does anybody have any ideas why a float of 88.339996 would convert to -2147483648 as a double?
I should also mention that I have tried using (double), for example "DTemp = (double)CurTemp;" which produced the same results.

Any help would be much appreciated. I don't have much experience programming with c or c++, but I understand most of the logic. Most of my programming experience is with vb.net.

Thanks for any help,

Andy

Edit: I should note that I'm using CentOS 5.3 with an AMD Athlon 1500+ processor.

Last edited by Ando140; 05-25-2009 at 10:48 AM.

 05-25-2009, 11:30 AM #2 norobro Member   Registered: Feb 2006 Distribution: Debian Sid Posts: 792 Rep: The problem is not with the conversion it is with your output format. "%d" outputs a signed decimal integer.
05-25-2009, 01:02 PM   #3
Ando140
LQ Newbie

Registered: Sep 2005
Posts: 9

Original Poster
Rep:
Ah, that makes sense. Thanks!
I was told (incorrectly apparently) to use %f for floats and %d for doubles. %f seems to give me the correct result for everything.

Next problem I'm having is these values don't seem to be getting passed correctly into a function I wrote.
Here's the function call:
Quote:
 weather_data.outHumidity = (int) (wvutilsCalcRelativeHumidity(DTemp, DDewpt));
The values passed in are as follows (reading from the output of the debug code above):
DTemp = 73.939995
DDewpt = 58.041893

Here's the code for the wvutilsCalcRelativeHumidity:
Quote:
 float wvutilsCalcRelativeHumidity (double temp, double dewpt) { double Tc, Tdc, E, Es, result; Tc = (temp - 32.0)/1.8; Tdc = (dewpt - 32.0)/1.8; Es = 6.11 * pow(10.0, (7.5 * (Tc/(237.7 + Tc)))); E = 6.11 * pow(10.0, (7.5 * (Tdc/(237.7 + Tdc)))); result = (E/Es) * 100.0; //DEBUG CODE FILE *OutF; OutF = fopen("/root/RHvals.txt", "w"); fprintf(OutF, "temp = %f\n", temp); fprintf(OutF, "dewpt = %f\n", dewpt); fprintf(OutF, "TempC = %f\n", Tc); fprintf(OutF, "DewptC = %f\n", Tdc); fprintf(OutF, "Es = %f\n", Es); fprintf(OutF, "E = %f\n", E); fprintf(OutF, "Result = %f\n", result); fclose(OutF); //END DEBUG return (float)result; }
The output from the debug code shows:
Quote:
 temp = -36893488147419103232.000000 dewpt = 3.288828 TempC = -20496382304121724928.000000 DewptC = -15.950651 Es = 193215165.036288 E = 1.764244 Result = 0.000001
Any ideas what's going on with the temp and dewpt variables?

 05-25-2009, 01:31 PM #4 norobro Member   Registered: Feb 2006 Distribution: Debian Sid Posts: 792 Rep: I don't see anything wrong with that code. What does the code look like between where DTemp is assigned a value and your function is called? This is weather related I can see. May I ask what is it that you are doing?
05-25-2009, 02:19 PM   #5
Ando140
LQ Newbie

Registered: Sep 2005
Posts: 9

Original Poster
Rep:
There is no code between that debug code and the function call. Here's what it looks like:

Quote:
 float CurTemp, CurDewpt; double DTemp, DDewpt; CurTemp = dest->outTemp; CurDewpt = dest->dewpoint; DTemp = CurTemp; DDewpt = CurDewpt; FILE *OutF; OutF = fopen("/root/wvBWvals.txt", "w"); fprintf(OutF, "temp= %f\n", dest->outTemp); fprintf(OutF, "dewpt= %f\n", dest->dewpoint); fprintf(OutF, "CurTemp= %f\n", CurTemp); fprintf(OutF, "CurDewpt= %f\n", CurDewpt); fprintf(OutF, "DTemp = %f\n", DTemp); fprintf(OutF, "DDewpt = %f\n", DDewpt); fclose(OutF); //END DEBUG //dest->outHumidity = (USHORT) wvutilsCalcRelativeHumidity(dest->outTemp, dest->dewpoint); dest->outHumidity = wvutilsCalcRelativeHumidity(DTemp, DDewpt);
I have a weather station and I'm using wview to create and upload a web page for the station. One of the problems with my station is that it has indoor himidity, but not outdoor. I'm making the assumption (which is one heck of an assumption and usually incorrect) that indoor and outdoor dewpoint are the same. So I calculate the indoor dewpoint and use that and the outside temperature to calculate the relative humidity outside, which gets used in other calculations too.

I'm puzzled as to why this isn't working.

 05-25-2009, 03:01 PM #6 norobro Member   Registered: Feb 2006 Distribution: Debian Sid Posts: 792 Rep: I copied your code and deleted a few lines: Code: #include #include float wvutilsCalcRelativeHumidity (double temp, double dewpt) { double Tc, Tdc, E, Es, result; Tc = (temp - 32.0)/1.8; Tdc = (dewpt - 32.0)/1.8; Es = 6.11 * pow(10.0, (7.5 * (Tc/(237.7 + Tc)))); E = 6.11 * pow(10.0, (7.5 * (Tdc/(237.7 + Tdc)))); result = (E/Es) * 100.0; //DEBUG CODE FILE *OutF; OutF = fopen("RHvals.txt", "w"); fprintf(OutF, "temp = %f\n", temp); fprintf(OutF, "dewpt = %f\n", dewpt); fprintf(OutF, "TempC = %f\n", Tc); fprintf(OutF, "DewptC = %f\n", Tdc); fprintf(OutF, "Es = %f\n", Es); fprintf(OutF, "E = %f\n", E); fprintf(OutF, "Result = %f\n", result); fclose(OutF); //END DEBUG return (float)result; } int main(int argc, char *argv[]) { float CurTemp=88.339996; float CurDewpt=55.805176; double DTemp, DDewpt; DTemp = CurTemp; DDewpt = CurDewpt; std::cout << "Return value=" << wvutilsCalcRelativeHumidity (DTemp, DDewpt) << std::endl; } At the console I got: Return value= 33.133 And in the output file: Code: temp = 88.339996 dewpt = 55.805176 TempC = 31.299998 DewptC = 13.225098 Es = 45.574062 E = 15.182085 Result = 33.312994 I don't know what the prob is either! What distro are you using? C++ I presume, what version? The reason that I asked what you were doing is that I have a Davis Vanage Pro II that I communicate with via a serial port. I have written c++/qt code to retrieve data from the console, put it on the screen and to store the data in a mysql database. Norm
 05-25-2009, 03:29 PM #7 Ando140 LQ Newbie   Registered: Sep 2005 Posts: 9 Original Poster Rep: Wish I could afford a Vantage Pro2, I've been looking at it for quite awhile. For now, I settled on what I think is the cheapest station I could find that would give me wind direction & speed, temperature, and rainfall. It was kind of an impulse buy too, just popped up on woot.com one day for \$60. It's a Thermor BIOS BW976. Wview doesn't naively support support this station, but a patch is available from the ws9xxd project on sourceforge. I applied that and decided I wanted a little more information, so I started modifying it. I'm not sure exactly what I'm using as far as compiling. I assume it's c and not c++, I think all it required was gcc. I don't really have anything good to debug this, I've just been using vi, so it's been a bit rough. I've been using putty to access the machine, nothing graphical. If you know of something I can use to debug it with putty, I'd love to give it a try. Other than that, I've just been doing the usual make and make install every time I make a code change, then looking at my output files to see what I've got. So I'm wondering if this could be the problem (wouldn't think it would be though). The code that calls the function is in ../stations/BW9xx/bw9xx.c and the actual function is in ../common/wvutils.c. I'm wondering if something could be happening between those points, though I don't know why it would. I'm going to try moving the function to the bw9xx.c file and see if it makes a difference. Thanks for looking into this, I do appreciate it. Andy
 05-25-2009, 03:46 PM #8 norobro Member   Registered: Feb 2006 Distribution: Debian Sid Posts: 792 Rep: I'm with you on the cost of the Vantage Pro II. I had been wanting one for quite a while but just couldn't justify the expense. Mine was a gift from my daughter; a combination birthday-father's day gift and pay back for some repair work to her house. I'll take a look at the wview files you listed and see if I can spot anything. Norm
 05-25-2009, 03:53 PM #9 Ando140 LQ Newbie   Registered: Sep 2005 Posts: 9 Original Poster Rep: Before ya get too far... I just found something that I had missed. There are 2 places where I call that function, one of them is inside the wvutils.c. I separated everything out and found that the first call, which is the one I was posting about, actually works correctly, but the second one was overwriting the file. So that one works, I gotta take a look at the other one and see what's going on.
 05-25-2009, 04:02 PM #10 norobro Member   Registered: Feb 2006 Distribution: Debian Sid Posts: 792 Rep: Another thing: take a look in sysdefs.h. All of those functions in wvutils.c are declared external there.
05-25-2009, 05:15 PM   #11
Ando140
LQ Newbie

Registered: Sep 2005
Posts: 9

Original Poster
Rep:
See if this one makes sense to you. The code for the relative humidity function is below. If I set a variable defined as a USHORT equal to that function, I get the correct value. However, if I assign a float to it, I get a value of 1.0000000. Any ideas?

Code calling funciton:
Quote:
 USHORT RelativeHumidity; RelativeHumidity = (float)wvutilsCalcRelativeHumidity(DTemp, DDewpt);
Function code:
Quote:
 float wvutilsCalcRelativeHumidity (double temp, double dewpt) { double Tc, Tdc, E, Es, result; Tc = (temp - 32.0)/1.8; Tdc = (dewpt - 32.0)/1.8; Es = 6.11 * pow(10.0, (7.5 * (Tc/(237.7 + Tc)))); E = 6.11 * pow(10.0, (7.5 * (Tdc/(237.7 + Tdc)))); result = (E/Es) * 100.0; return (float)result; }

 05-25-2009, 05:49 PM #12 Ando140 LQ Newbie   Registered: Sep 2005 Posts: 9 Original Poster Rep: alright, ignore the previous post, I seem to have it working correctly now. I don't know what that external stuff does, but I checked out that sysdefs.h file and added my functions to it and it seems to be fixed now. Thanks again for your help. Andy
05-25-2009, 06:20 PM   #13
norobro
Member

Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep:
You're welcome.

A definition of extern from here :
Quote:
 The extern keyword is used to inform the compiler about variables declared outside of the current scope. Variables described by extern statements will not have any space allocated for them, as they should be properly defined elsewhere. Extern statements are frequently used to allow data to span the scope of multiple files.
If you have more problems, send me a visitor message and I'll try to help.

Norm

Last edited by norobro; 05-25-2009 at 06:45 PM. Reason: pasted definition twice

05-25-2009, 06:30 PM   #14
jiml8
Senior Member

Registered: Sep 2003
Posts: 3,171

Rep:
Quote:
 Does anybody have any ideas why a float of 88.339996 would convert to -2147483648 as a double?
It might have something to do with you printing your double using a %d specification.

Note: "might have something to do..." is a massive understatement.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post valembe Programming 10 11-03-2008 12:16 PM C_to_be Programming 2 10-29-2007 06:41 PM pirate_pete Programming 5 09-15-2005 02:00 AM ashirazi Programming 6 11-30-2004 04:14 PM Mega Man X Programming 16 01-03-2004 07:50 PM

LinuxQuestions.org

All times are GMT -5. The time now is 06:46 PM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -