can someone show me some code to convert rankine temperature to kelvin? rankine is in the same scale as fahrenheit, but it starts at absolute zero. so 0R = -459.67F, heres is what I have

convert to kelvin

def __init__(self, value=0):
        Fah=value-459.69
        Cel=(Fah-32)*5/9.0
        value=Cel-273.15
        Temperature.__init__(self, value)

convert back to rankine

def __str__(self):
        Cel=self.value+273.15
        Fah=Cel*1.8+32
        self.value=Fah+459.69
        return Temperature.__str__(self, 'R')

for anyone wondering, this is an assignment where I have to store a temperature using a base value (i decided the base will be in kelvin degrees) in a class called "Temperature()"
I have child classes for kelvin, celsius, fahrenheit, and rankine. I have to be able to store all the temperature in the kelvin equivalent and then be able to print them out in their native (such as celsius, or fahrenheit) values. so if I did

a=TempK(100)
print a

it will print
The Temperature is = 100K

if I do

b=TempC()
print b

it will say
The Temperature is = 0C

and if I do

b.set(a)
print a

it is supposed to set a (TempC()) to the kelvin equivalent of whatever b is (TempK()) and then print it out in celsius. which would do this
The Temperature is = -173.15C

I have fahrenheit, celsius and kelvin all working, I just can't seem to get rankine to convert correctly

I would recommend re-ordering your class a bit, if you have the freedom to do so:

class Temperature(object):
     def __init__(self, temp):

     def toCelcius(self):
   
     def toRankine(self):
 
     def toFahrenheit(self):

That way, all of your temperatures can be stored as objects of the same class and you can access the value in any unit you desire.

You could add additional methods to convert fromCelcius, fromRankine, and fromFahrenheit if you wanted.

Anyways, to the topic:

Your Kelvin <--> Celcius conversions are backwards in both code snippets. The first should read:

def __init__(self, value=0):
        Fah=value-459.69
        Cel=(Fah-32)*5/9.0
        value=Cel+273.15  ## <<<<
        Temperature.__init__(self, value)

and the second,

def __str__(self):
        Cel=self.value+273.15  ## <<<
        Fah=Cel*1.8+32
        self.value=Fah+459.69
        return Temperature.__str__(self, 'R')

Here's a fun algebra exercise: can you reduce both of these snippets to a single formula (for the sake of efficiency)?

Jeff

okay I'll give this a try and see how it works out. I was actually hoping I could do them all as one class so it would be simple like you're saying, but unfortunately i'm learning about class inheritance so that is out of the question :( and uhh, no I don't think I could reduce this if I tried lol.

an someone show me some code to convert rankine temperature to kelvin? rankine is in the same scale as fahrenheit, but it starts at absolute zero. so 0R = -459.67F, heres is what I have

So rankine would be fahrenheit -459.67 the way I read what you have said

def to_rankine_from_celsius( tempC ):
   fah = convert_to_fahrenheit_from_celsius(tempC)
   return fah-459.67
#
def from_rankine_to_celsius( tempR ):
   cel = convert_from_fahrenheit_to_celsius(tempR+459.67)
   return cel
#
##   In python you can call arguments individually if this will make it easier
##   The formula for celsius <---> fahrenheit can also be stated as 9C + 160 = 5F
def Cel_Fah( cel=-999, fah=-999):
   if fah > -999:
      ## 9C + 160 = 5F
      ## 9C = 5F - 160
      c9 =(5 * fah) - 160
      c = float(c9) / 9
      print c
      
   if cel > -999:
      ## 9C + 160 = 5F
      ## 5F = 9C + 160
      f5 =(9 * cel) + 160
      f = float(f5) / 5
      print f

Cel_Fah( fah=100 )
Cel_Fah( cel=30 )

Okay I have it all working except for with zero on my fahrenheit and rankine, heres my out puts

>>> a=TempF(0)
>>> print a
The Temperature is = 1.06581410364e-014F
>>> b=TempR(0)
>>> print b
The Temperature is = -5.68434188608e-014R

whats with all the excess numbers and stuff? heres my actual code for fahrenheit and rankine

class TempF(Temperature):
    ''' TempF() - This is the Fahrenheit class. It is a child of Temperature() and it controls
        everything using the Fahrenheit standard
    '''
    def __init__(self, value=32):
        Cel=(value-32)*5/9.0
        value=Cel+273.15
        Temperature.__init__(self, value)

    def __str__(self):
        Cel=TempF.getValue(self)-273.15
        value=Cel*1.8+32
        return Temperature.__str__(self, value, 'F')

    def getValue(self):
        return Temperature.getValue(self)

    def setValue(self, value=0):
        value=5*(value-32)/9+273.15
        Temperature.setValue(self, value)

class TempR(Temperature):
    ''' TempR() - This is the Rankine class. It is a child of Temperature() and it controls
        everything using the Rankine standard
    '''
    def __init__(self, value=0):
        Fah=value+459.67
        Cel=(Fah+32)*5/9.0
        value=Cel-273.15  
        Temperature.__init__(self, value)  

    def __str__(self):
        Cel=TempR.getValue(self)+273.15
        Fah=Cel*1.8-32
        value=Fah-459.67
        return Temperature.__str__(self, value, 'R') 

    def getValue(self):
        return Temperature.getValue(self)

    def setValue(self, value=0):
        Fah=value+459.67
        Cel=(Fah+32)*5/9.0
        value=Cel-273.15  
        Temperature.setValue(self, value)

You're experiencing the imprecisions of computer arithmetic -- it happens in any language and is the combined result of base 2 arithmetic AND the impossibility of writing repeating decimals with a finite amount of memory (or time!).

The way to pretty it up is like this:

return "%0.1f" % value

This interpolates a floating point number with 0 leading zeroes and 1 decimal point. It will have the effect of rounding your value to the nearest 0.1.

Jeff

This article has been dead for over six months. Start a new discussion instead.