I wouldn't recommend messing around with ": .

   exponent=: 10&(<.@^.)
   mantissa=: % 10^exponent

Give you the numerical values of the exponent and mantissa. Then if you need
a string, use ": , with an appropriate left argument if you want to control
the number of digits.

Marshall

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of PackRat
Sent: Tuesday, March 29, 2011 6:51 PM
To: Programming forum
Subject: [Jprogramming] Extracting from scientific notation

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

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to