This has got to be easy, but I couldn't find anything easily
discernable from the Vocabulary page or from the Phrases pages:
I'm having problems extracting the mantissa and the exponent from a
number in scientific notation. I'm trying to compare numbers of
differing magnitudes to see how similar the digits of the numbers are
regardless of the magnitude. To do so, I'm converting them to
scientific notation to separate the magnitude from the base value,
using this little converter to scientific notation:
sn=. 0j_3 ": ]
QUESTION 1: The converter requires a specification of the number of
digits to retain in the result (in this case, 3). Instead of a
specified fixed value, how does one specify to the interpreter to use
whatever number of digits there are in the argument? (In other words,
the converter should depend upon the input, not upon the programmer.)
The following verb phrase extracts the mantissa from a scientific
notation equivalent of the argument (using the above converter):
((sn y) i. 'e') {. (sn y)
If y = 440.5 , then the above converts it to '4.405e2' and correctly
extracts the digits ahead of the 'e': '4.405' .
QUESTION 2: However, if the two verb phrases above are incorporated
into an explicit verb in a script:
mantissa=. 3 : 0
sn=. 0j_3 ": ] NB. convert to scientific notation as a literal
((sn y) i. 'e') {. (sn y)
)
then it fails with the following error after loading the script
containing the above verb and then executing the verb with a value:
mantissa 440.5
|syntax error
| mantissa 440.5
Obviously, I must be doing something wrong, but I can't see what the
the error is in the explicit verb definition, since both lines within
the verb work perfectly well when entered directly into the
interpreter.
I have a companion explicit verb to extract the exponent:
exponent=. 3 : 0
sn=. 0j_3 ": ] NB. convert to scientific notation as a literal
(1+(#(mantissa y))) }. (sn y)
)
but it depends upon the "mantissa" verb earlier, which is currently
giving the above error message.
Can anyone help the blind see? Thanks in advance!
Harvey
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm