[Edu-sig] Re: (financial calculation) - using units

Dethe Elza delza at livingcode.org
Wed Feb 16 04:32:22 CET 2005


On 15-Feb-05, at 12:51 PM, Christian Mascher wrote:
> BTW, how does one express units of measure in Python/programming? 
> Calculations in applications aren't just about numbers. For instance, 
> how could one express equations like
> 	10000 cm^2 == 1 m^2
> 	1000 g == 0.001 t
> in a programming language? Another point would be the inclusion of a 
> measure of accuracy:
> For a physicist or engineer
> 	1 cm != 1.00 cm
> because the first could be 0.9 cm, the second couldn't, beeing more 
> accurate.

You might want to take a look at Frink.  Programming explicitly in 
units is it's main purpose, although it also contains lots of reference 
data as part of it's domain intelligence (including WordNet, the 
open-source dictionary (I'm oversimplifying, WordNet is more than a 
dictionary)).

http://futureboy.homeip.net/frinkdocs/index.html

 From the Frink documentation:
<quote>
  I received one of those endlessly-forwarded  e-mails of dubious but 
"interesting facts" which said "if you fart  continuously for 6 years 
and 9 months, you'll have enough gas to create the equivalent of an 
atomic bomb." Hee hee. Cute. (Thanks to Heather May  Howard... being 
unable to easily calculate the veracity of this statement  was one of 
the primary influences that showed how existing programs were  too 
limited and inspired the creation of Frink.) But I didn't believe it  
and wanted to check it. The Hiroshima bomb had a yield of 12.5 kilotons 
of TNT, which is a very small bomb by today's standards. How many 
horsepower  would that be?

  12.5 kilotons TNT / (6 years + 9 months) -> horsepower
329.26013859711395

  Can you produce a 329-horsepower blowtorch of a fart? I doubt it.  
That's the power produced by a Corvette engine running just at its 
melting  point. A one-second fart with that much power could blow me 
1000 feet  straight up. To produce that kind of energy, how much food 
would you have  to eat a day?

  12.5 kilotons TNT / (6 years + 9 months) -> Calories/day
5066811.55086559

  Ummm... can you eat over 5 million Calories a day? (Again, note  that 
these are food Calories with a capital 'c' which are equal to  1000 
calories with a small 'c'.) If you were a perfect fart factory,  
converting food energy into farts with 100% efficiency, and ate a 
normal  2000 Calories/day, how many years would it really take?

  12.5 kilotons TNT / (2000 Calories/day) -> years
17100.488984171367

  17,000 years is still a huge underestimate; I don't know how much of 
your  energy actually goes into fart production. Oh well. To continue 
the  calculations, let's guess your butthole has a diameter of 1 inch 
(no,  you go measure it.) Let's also guess that the gas you actually  
produce in a fart is only 1/10 as combustible as pure natural gas. What 
  would be the velocity of the gas coming out?

  12.5 kilotons TNT / natural_gas / (6 years + 9 months)  / (pi (.5 
in)^2) 10 -> mph
280.1590446203110

  Nobody likes sitting next to a 280-mile-per-hour fart-machine.  
Lesson: Even the smallest atomic bombs are really  unbelievably 
powerful and whoever originally calculated this isn't any fun to be 
around if they really fart that much.

  Fart jokes. Sheesh. If Frink isn't a huge success, it's not because I  
didn't pander to the Lowest Common Denominator.
</quote>

There are lots of more practical usages as well.  Frink syntax is 
somewhere in the Perl/Python ballpark, but explicitly intended for 
one-off short scripts.  It's written in Java, so fairly portable.  And 
the data sets it incorporates are valuable in themselves.

I'd love to see a Python version of this, but since it can be embedded 
in Java with two lines of code, it should be accessible from Jython.  
Here are a few more of the features, also from the docs:

<quote>
     Tracks units of measure (feet,  meters, tons, dollars, watts, etc.) 
through all calculations and allows  you to add, subtract, multiply, 
and divide them effortlessly, and makes sure the answer comes out 
correct, even if you mix units like gallons and  liters.
	* 	 Arbitrary-precision math, including huge  integers and 
floating-point numbers, rational numbers (that is, fractions  like 1/3 
are kept without loss of precision,) and complex numbers.
	* 	 Advanced mathematical functions  including trigonometric functions 
(even for complex numbers,) factoring and primality testing, and base 
conversions.
	* 	 Unit Conversion between thousands of unit  types with a huge 
built-in data  file.
	* 	 Date/time math (add offsets to dates,  find out intervals between 
times,) timezone conversions, and  user-modifiable date formats.
	* 	 Translates between several human  languages, including English, 
French, German, Spanish, Portuguese, Dutch,  Korean, Japanese, Russian, 
Chinese, Swedish, and Arabic.
	* 	 Calculates historical buying power of the U.S. dollar and British 
pound.
	* 	 Calculates exchange rates  between most of the world's currencies.
</quote>

HTH

--Dethe

"Say what you like about C++, but it's uninitialized variables will 
always
hold a special place in my heart. In a world where we define 
*everything*
concretely it is the last refuge of the undefined. It's the programmer's
Wild West, the untamed frontier." --Bjorn Stroustrap
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2488 bytes
Desc: not available
Url : http://mail.python.org/pipermail/edu-sig/attachments/20050215/88783359/smime.bin


More information about the Edu-sig mailing list