Articles Archive
Articles Search
Director Wiki
 

Using Scientific Numbers with Director

July 11, 2002
by Will Turnage

Hi Handyman,

I am still trying to write a handler to control scientific numbers. For example :

0.00000007 = 7 e -8

Can you help me?

Kind regards

Saleh

Dear Saleh,

Well, the good news is that Director can natively process scientific numbers. The key to doing this is to type a single number followed by a lowercase 'e' and then a second integer representing the exponent. You can try this yourself by opening the message window and typing the following lines:

put 7.32e18 * 6.853e-17
-- 501.6396
put 3.74e96 * 9.3245e-92
-- 348736.3000

The results here are displayed as normal numbers because their values are relatively small. However, if you were to take a larger mathematical computation like this:

put 4.657e34 / 8.113e-15
-- 5.74017009737458e48

Then, you'll notice that Director displays the results in scientific notation. The big question you're probably asking now is how do you control whether Director displays a normal number or scientific notation? The answer is, it depends on the value of your number.

If your number is greater than zero, then the rule is that Director will display a maximum of 16 digits in your number. If your number requires more than 16 digits to display properly, then Director will display the results in scientific notation. Look at this sequence for an example,

put 7.64e9
-- 7640000000.0000
put 7.64e10
-- 76400000000.0000
put 7.64e11
-- 764000000000.0000
put 7.64e12
-- 7.64000000000000e12
put 7.64e13
-- 7.64000000000000e13

In this sequence of numbers, you keep incrementing the exponent by one. If the resulting number contains less than 16 digits, then Director displays the full number. Otherwise, it uses scientific notation.

The important thing to remember here is that the 16 digits Director can display includes the numbers after the decimal point. So the way Director displays the numbers will vary based on what you have the floatPrecision set to: For instance,

the floatPrecision = 4
put 7.64e9
-- 7640000000.0000
the floatPrecision = 6
put 7.64e9
-- 7640000000.000000
the floatPrecision = 8
put 7.64e9
-- 7.64000000000000e9

In this example, you are putting the same number over and over, but by changing the value of the floatPrecision, you're also changing the number of digits that are displayed. So in the last example when you set the floatPrecision to 8, then your number will have more than 16 digits to display, thus Director will use scientific notation.

Up until this point, you've only been dealing with numbers that are greater than zero. For numbers less than zero, the rules change significantly. For these really small numbers, the only way to get Director to display them in scientific notation is to set the floatPrecision to its maximum value of 15. For instance,

the floatPrecision = 11
put 7.32e-11
-- 0.00000000007

the floatPrecision = 13
put 7.32e-11
-- 0.0000000000732

the floatPrecision = 15
put 7.32e-11
-- 7.32000000000000e-11

You'll notice in the first example of this list that the number appears to be rounded off. Keep in mind, that Director is not really rounding off your number, it's only rounding it off for display purposes. The full value of your number is still correctly stored in memory. For example,

the floatPrecision = 8
tempNum = 7.32e-11
put tempNum * 345
-- 0.00000003
put tempNum * 3450
-- 0.00000025

So Director has the ability to natively read scientific notation and store its correct value in memory. It also has the ability to display numbers in scientific notation. However, what Director does NOT have the capability to do is to give you control over how the scientific number is displayed. That's where you'll have to write your own Lingo. Luckily, the Handyman has already done it for you.

on convertToScientificNotation tempFloat, significantDigits

  tempFloat = value (tempFloat)
  exponent = 0
  if tempFloat < -1.0 then
    repeat while tempFloat <= -10.0
      exponent = exponent + 1
      tempFloat = tempFloat / 10.0
    end repeat
  else if tempFloat < 0.0 then
    repeat while tempFloat > -1.0
      exponent = exponent - 1
      tempFloat = tempFloat * 10.0
    end repeat
  else if tempFloat < 1.0 then
    repeat while tempFloat < 1.0
      exponent = exponent - 1
      tempFloat = tempFloat * 10.0
    end repeat
  else
    repeat while tempFloat >= 10
      exponent = exponent + 1
      tempFloat = tempFloat / 10.0
    end repeat
  end if
  significantDigits = max (0, integer (significantDigits))
  the floatPrecision = min (14, significantDigits)
  return string (tempFloat) & "e" & string (exponent)

end

There are two parameters that have been passed to this handler. The first is the number that you want to convert to scientific notation, and the second value is the number of significant digits that you want displayed in the final scientific notation. The handler begins by getting the value of the number that you passed. If you actually passed a number to the handler, then this step doesn't do anything. However, if you passed a string to the handler, then it gets converted to a number in this step. Next, you initialize the exponent variable to be zero.

The next step of the handler takes into account for different possibilities for your number. The first possibility is that the base number is negative, but your exponent is positive (meaning your number is less than or equal to -1). The next option is that your base number is negative and your exponent is negative (meaning your number is between -1 and 0). The next option is that your base number is positive and your exponent is negative (meaning your number is between 0 and 1). And the last option is that your base number is positive and your exponent is positive (meaning your number is greater than or equal to 1). For each one of these possibilities you repeat through a loop either multiplying the number by 10 or dividing it by 10 until your base is a number between 1 and 10. For each iteration, it keeps track of this by either increasing or decreasing the exponent.

When this is completed, then you make sure that the significantDigits variable is a valid integer between 0 and 14. Then you set the floatPrecision to that number, and finally you return a string consisting of the base, the letter e, and the exponent.

When all is said and done, then here are some of the results:

put convertToScientificNotation (5464356345, 5)
-- "5.46436e9"
put convertToScientificNotation (-546435.234, 1)
-- "-5.5e5"
put convertToScientificNotation (0.00059115, 3)
-- "5.912e-4"
put convertToScientificNotation (-0.004992468, 2)
-- "-4.99e-3"
put convertToScientificNotation ("550087e12", 2)
-- "5.50e17"

All colorized Lingo code samples have been processed by Dave Mennenoh's brilliant HTMLingo Xtra, available from his site at http://www.crackconspiracy.com/~davem/

Will Turnage is a multimedia programmer based in New York City. In addition to his weekly role as Director Online's Multimedia Handyman, he is also the Technology Director for Sony Music's Client Side Technologies Group. You can read more about his work at http://will.turnage.com/.

Copyright 1997-2017, Director Online. Article content copyright by respective authors.