# Math conversion API.
# Rick Smereka, Copyright (C) 2005.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, get a copy via the Internet at
# http://gnu.org/copyleft/gpl.html or write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA

# You can contact the author via email at rsmereka@future-lab.com

# The procedures in this module perform math conversions. Any
# calculation that that supports floating point numbers will
# use the fixed point math API ('fxp.tcl').

# Initial tcl version. Apr/2005, Rick Smereka

proc conv_fah_2_cel {s1} {
   # Convert a temperature from fahrenheit to celisus. The
   # fahrenheit is expected in 's1' as an 'fxp' list
   # (see 'fxp.tcl'). Returns an 'fxp' list representing
   # the converted temperature. Formula used is:

   #   (far - 32) / 1.8
   set mname "conv_fah_2_cel"
   logman "$mname:enter"

   if {![fxp_get_list $s1 s1_num s1_scale]} {
      logman "$mname:exit:empty or invalid(s1)"
      return ""
   }

   set f1 [fxp_iconv 32]

   if {$f1 == ""} {
      logman "$mname:bad rc from fxp_iconv(f1)"
      return ""
   }

   set f2 [fxp_sub $s1 $f1]

   if {$f2 == ""} {
      logman "$mname:bad rc from fxp_sub(s1,f1)"
      return ""
   }

   set f1 [fxp_iconv "1.8"]

   if {$f1 == ""} {
      logman "$mname:bad rc from fxp_iconv(1.8)"
      return ""
   }
   
   set f3 [fxp_div $f2 $f1]

   if {$f3 == ""} {
      logman "$mname:bad rc from fxp_div(f1,f2)"
      return ""
   }
   
   logman "$mname:normal exit(TRUE)"
   return $f3
}

proc conv_cel_2_fah {s1} {
   # Convert a temperature from celisus to fahrenheit. The
   # celisus temperature is expected in 's1'. Upon success,
   # the converted fahrenheit temperature will be return as
   # an 'fxp' list. Returns the converted temperature upon
   # success, an empty string otherwise. Formula used is:

   #     cel * 1.8 + 32
   set mname "conv_cel_2_fah"
   logman "$mname:enter"

   if {![fxp_get_list $s1 s1_num s1_scale]} {
      logman "$mname:exit:empty or invalid(s1)"
      return ""
   }

   set f1 [fxp_iconv "1.8"]

   if {$f1 == ""} {
      logman "$mname:bad rc from fxp_iconv(1.8)"
      return ""
   }

   set f2 [fxp_mul $s1 $f1]

   if {$f2 == ""} {
      logman "$mnme:bad rc from fxp_mul(s1,f1)"
      return ""
   }

   set f1 [fxp_iconv 32]

   if {$f1 == ""} {
      logman "$mname:bad rc from fxp_iconv(f1)"
      return ""
   }

   set f3 [fxp_add $f2 $f1]

   if {$f3 == ""} {
      logman "$mname:bad rc from fxp_add(f2,f1)"
      return ""
   }
   
   logman "$mname:normal exit(TRUE)"
   return $f3
}
