> all these are evaluated by $Calc with the second one (1.43e9) being the
Agreed, although not all people might know that notation.
> When I convert from wavelength to frequency, the result is in standard
> notation, not scientific. 21 cm = 1427583133.333
> imagine it would be tricky to convert the result to scientific notation.
If you mean just the exponent notation, that should be pretty straight
forward. For understanding SI-units you would probably want to get a
separate program and control it in your clip. I think the Unix program units
might be able to do the conversions. There are windows versions as well.
I can think of two ways of converting to scientific notation. THe first uses
a classic function called sprintf short for formatted print to String. It's
built-in in Perl and Python, is in the standard library in C (in which it
originated) and Java as well as being available to a huge number of other
langs. So does NoteTab have a sprintf function, by any chance?
Saying something like
$variable = sprintf("%.2g", 1427583133.333);
would do the job. %g says the next argument to the function should be
printed out in scientific notation and the number after the period gives the
precision. The above would place the string (text):
in the variable $variable. The above syntax is Perl but basic sprintf
formatting works the same across languages.
There's a manual method. I'm no mathematician but here's my initial attempt
that seems to get most of the cases:
For simplicity suppose the input number is greater than 0 and might or might
not include a period I.e. a fractional part.
Here's a partly pseudo-code algorithm for arriving at the scientific
1. Compute the exponent like this:
$exponent = floor(log10($number));
The rationale here is as follows:
Log is the inverse function of exp so log10 tells you the exponent to which
a base 10 number needs to be raised to be equal to the input value. However,
the whole point of scientific notation is to use integer exponents. THus
round down any fractional part using floor.
2. Next evaluate:
$number = $number / 10 ** $exponent;
Where ** is the power of operator (^ in NoteTab). That is scientific
notation is usually writtten:
d.dddd where d stands for a digit. We need to get the number into a form
that's got only one digit before the fractional part. Dividing it by raising
10 to the power of the exponent seems to get you back to such a
presentation. In other words you are moving the period right or left by
dividing or multiplying by 10 repeatedly, from a conceptual stand point. As
I said I'm better with logic, as needed in programming, than pure math.
3. THe last step of processing you might need to do is to ensure that even
if the division in the previous step produced an integer (i.e. log10
returned an integer), we still add a period and a trailing 0. YOu do this by
scanning the string for a period and if It does not have one, you append the
string ".0" at the end of the input. Written in Perl that step would look
$number .= '.0' if index($number, '.') == -1;
So if we had 1000 you would get 3 as the exponent and after division would
have 1. You would need to add ".0" to it to make it look consistent 1.0 that
4. Now just print the string. It is the concatenation of the string variable
number, the exponent 'e',and the value of the variable named exponent.
I guess you could try converting the above algorithm to clip code. I'm
posting this at the dead of night Finnish local time and just quickly tested
the code in Perl. So human and or logical errors may still remain. And once
again I don't know any higher math period.
With kind regards Veli-Pekka Tätilä (vtatila@...
Accessibility, game music, synthesizers and more: