Bay Six Software Forum Index Bay Six Software
Beyond the Basics
 
 FAQFAQ   SearchSearch   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Experiment - Units of measure

 
Post new topic   Reply to topic    Bay Six Software Forum Index -> ViviFire language
View previous topic :: View next topic  
Author Message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 729

PostPosted: Apr 21st, 2012, 5:40pm    Post subject: Experiment - Units of measure Reply with quote

VF will have support for units of measure. This means that instead of using raw numbers for things such as angles in radians in trig functions, VF will require the programmer to specify the unit when one is expected by a function, et cetera. This may sound onerous at first, but it offers some advantages such as automatic conversion if the given unit is in the same "dimension" as the expected unit.

The syntax is still a little rough around the edges, but works like this. The Base Unit statement defines a dimension and specifies its base unit from which other units can be derived. The Unit statement defines a unit and may include a conversion formula. There are ways to specify (1) that the unit is defined by the SI; (2) aliases, such as meter vs metre and meter vs meters vs m. A consequence of declaring an SI unit is the automatic creation of conversion factors of 10 with the standard prefixes, such as Kilometer and km. While VF's keywords and identifiers are case-insensitive, units are case-sensitive, just as they are in the real world.

Rather than giving the grammar, I'll just post my test file for you all to look over and give your opinions about it.
Code:
module StdUnits

base unit length in meters
unit -meter(s) metre(s) m
unit inch(es) = 2.54cm
unit foot feet ft = 12inches
unit yard(s) yd(s) = 3feet
unit mile(s) mi = 5280feet
unit light_year(s) ly = 9.4605284 petameters

base unit time in seconds
unit -second(s) sec(s) s
unit minute(s) min(s) m = 60secs
unit hour(s) h = 60mins

base unit speed in meters/second = length/time
unit MPH = miles/hour

base unit angle in radians
unit radian(s) rad
unit degree(s) deg = 0.0174532925 radians
unit gradian(s) grad = 0.9 degrees

base unit temperature in Kelvin
unit Kelvin K
unit degree_C degrees_C += 273.15 Kelvin
unit degree_F degrees_F = 1.8 degrees_C + 32
unit Rankine R = (0 degrees_C + 273.15) * 1.8

base unit area in meters2 = meters * meters
unit -meter2 meters2 metre2 metres2 m2
unit hectare ha = 10000 meters2
unit square_foot square_feet sq_ft = 0.09290304 meters2

base unit seconds_squared in seconds2 = seconds * seconds
unit second2 seconds2 sec2 secs2 s2

base unit acceleration in meters/second2 = length/time/time
unit Earth_gravity g = 9.80665 m/s2

base unit mass in kilograms
unit -gram(s) g
unit metric_ton(s) metric_tonne(s) t = 1000 kilograms

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
STPendl
Full Member


Joined: 20 Aug 2007
Posts: 161
Location: Austria

PostPosted: Apr 21st, 2012, 7:24pm    Post subject: Re: Experiment - Units of measure Reply with quote

Code:

unit degree_F degrees_F = 1.8 degrees_C + 32
unit Rankine R = (0 degrees_C + 273.15) * 1.8

Isn't there an asterisk missing between 1.8 and degrees_C, similar in the second line after zero?

The rest has to sink in, but it seems to be a good addition.

_________________
Stefan

Any code I post can be freely used, just give credit.
Back to top
View user's profile Send private message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 729

PostPosted: Apr 21st, 2012, 9:56pm    Post subject: Re: Experiment - Units of measure Reply with quote

Yes, that does look incorrect and I'm not certain what syntax would be clean and correct. My syntax is based on Fortress, but I'm having trouble finding a source file that may have these units defined.

For comparison, another current language that supports units is F#. However F# doesn't have a concept of a "base unit", so conversions must be written to go both ways.
Code:
[<Measure>] type degC // temperature, Celsius/Centigrade
[<Measure>] type degF // temperature, Fahrenheit

let convertCtoF ( temp : float<degC> ) = 9.0<degF> / 5.0<degC> * temp + 32.0<degF>
let convertFtoC ( temp: float<degF> ) = 5.0<degC> / 9.0<degF> * ( temp - 32.0<degF>)

// Define conversion functions from dimensionless floating point values.
let degreesFahrenheit temp = temp * 1.0<degF>
let degreesCelsius temp = temp * 1.0<degC>

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 729

PostPosted: Apr 22nd, 2012, 1:01am    Post subject: Re: Experiment - Units of measure Reply with quote

All I can find of Fortress are
Code:
dim Temperature  SI_unit kelvin kelvins K_
in one file, and in another
Code:
(* We assume that an absolute temperature of 0 degrees Smurdley is the same as
   an absolute temperature of 0 degrees Celsius.  Then a relative temperature
   of 1 degree Smurdley is 27/100 degree Celsius = 0.27 kelvin. *)

unit degreeSmurdley: Temperature = 0.27 kelvin
which isn't much help.
So I then looked up what Frink does. http://futureboy.us/frinkdata/units.txt which defines temperature units, but gives the caveat that they should only be used with calculations involving changes in temperature. A conversion between different scales requires a function call.

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
STPendl
Full Member


Joined: 20 Aug 2007
Posts: 161
Location: Austria

PostPosted: Apr 22nd, 2012, 7:17am    Post subject: Re: Experiment - Units of measure Reply with quote

I was wondering what the term "1.8 degrees_C" would do.

0 Kelvin is the absolute zero temperature, so this is the base, which is understandable.

Conversion from 0 Kelvin to 0 Celsius is done by adding 273.15 this I have been taught too.

But conversion from 0 Celsius to 0 Fahrenheit is where I get stuck, so does the line tell us that "1.8C + 32 = 33.8C" are 0 Fahrenheit?

_________________
Stefan

Any code I post can be freely used, just give credit.
Back to top
View user's profile Send private message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 729

PostPosted: Apr 22nd, 2012, 6:34pm    Post subject: Re: Experiment - Units of measure Reply with quote

Based on what other languages are doing, I have decided to do some sort of quasi-function syntax. However, I'm not sure where to place the parameter, before the name(s) of the unit or after. The first way more resembles how the unit is used, but puts a bit of "noise" up front. Do any of you have an opinion on which is more readable?
Code:
' #1
unit [k in Kelvin] degree_C degrees_C = k+273.15
unit [c in degrees_C] degree_F degrees_F = 1.8*c+32
unit [k in Kelvin] Rankine degree_R degrees_R = 1.8*k

' #2
unit degree_C degrees_C [k in Kelvin] = k+273.15
unit degree_F degrees_F [c in degrees_C] = 1.8*c+32
unit Rankine degree_R degrees_R [k in Kelvin] = 1.8*k

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
STPendl
Full Member


Joined: 20 Aug 2007
Posts: 161
Location: Austria

PostPosted: Apr 22nd, 2012, 7:25pm    Post subject: Re: Experiment - Units of measure Reply with quote

I think #1 is better, since the variable declaration is placed before the unit definition, which in turn is more an expression than a declaration.
_________________
Stefan

Any code I post can be freely used, just give credit.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Bay Six Software Forum Index -> ViviFire language All times are GMT
Page 1 of 1
Jump to:  
Quick Reply
Username:
Message:
   Shortcut keys: Alt+Q to activate, Alt+P to preview, Alt+S to submit
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum



Lo-Fi Version
Powered by phpBB © 2001, 2005 phpBB Group