Converting from fahrenheit to celcius problem.

Everything else that doesn't fall into one of the other PB categories.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Converting from fahrenheit to celcius problem.

Post by GeoTrail »

I'm having some problems when converting from celcius to fahrenheit.
When I input 22 celcius it gives back 71. Think that is correct.
But when I try to convert 71 fahrenheit to celcius it returns 21 instead of 22. Could anyone help me with this?

Code: Select all

Procedure.l celcius2fahrenheit(in.l)
  Result.l = (in.l * 9/5) + 32
  ProcedureReturn Result.l
EndProcedure

Procedure.l fahrenheit2celcius(in.l)
  Result.l = (in.l-32) * 5/9
  ProcedureReturn Result.l
EndProcedure

Debug celcius2fahrenheit(22)
Debug fahrenheit2celcius(71)
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

You want to code with floats? So why don't you do it :D

Code: Select all

Procedure.l celcius2fahrenheit(in.l) 
  Result.l = (in.l * 9.0/5.0) + 32 
  ProcedureReturn Result.l 
EndProcedure 

Procedure.l fahrenheit2celcius(in.l) 
  Result.l = (in.l-32) * 5.0/9.0
  ProcedureReturn Result.l 
EndProcedure 

Debug celcius2fahrenheit(22)
Debug fahrenheit2celcius(71)
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Was that all? :lol:
Thanks, I didn't even think about that.

Cheers m8 :)
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

According to both http://www.onlineconversion.com/temperature.htm and
http://www.allmeasures.com/temperature.html we see that 22 c = 71.6 f,
and that 71.6 f = 22 c. So here's my offering that does the job:

Code: Select all

Procedure.s c2f(c.f)
  ProcedureReturn StrF((c*9/5)+32,1)
EndProcedure

Procedure.s f2c(f.f)
  ProcedureReturn StrF((f-32)*5/9,1)
EndProcedure

Debug c2f(22)
Debug f2c(71.6)
(@Freedimension: Your code is wrong -- it returns 72 instead of 71.6). ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

PB wrote: (@Freedimension: Your code is wrong -- it returns 72 instead of 71.6). ;)
Darn, i knew i missed something :D
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Great, thanks alot PB.
The more accurate the better ;)
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
Post Reply