WeBWorK Problems

extracting mantissa and exponent from float values?

extracting mantissa and exponent from float values?

by Zak Zarychta -
Number of replies: 3
Is there a way using contexts to extract the mantissa and exponent from a arbitrary float value?

For example, given the numbers 8.98e9 or 8980000000 that are arbitrarily calculated from other values and assigned to the variable $k can you set variables such that

$mant_k = mantissa($k); # =8.98
# and
$exp_k = exponent($k); # =9

These are available in the core module math::BigFloat but I get an error trying to load this in $WW. Any ideas?

Thanks in advance,
Zak
In reply to Zak Zarychta

Re: extracting mantissa and exponent from float values?

by Alex Jordan -
I have some questions (not that I will have an answer for you either way, just curious).

Would negative numbers give a negative mantissa?

What would come from $k = 0 ?

If $k was defined to be 8.980e9, would you expect the mantissa to be 8.980?

If $k were defined to be 8980000000 and the intent was that any number of those 0s were significant, would you still expect to see 8.98 as a mantissa?
In reply to Alex Jordan

Re: extracting mantissa and exponent from float values?

by Zak Zarychta -
I have some questions (not that I will have an answer for you either way, just curious).

Would negative numbers give a negative mantissa? Yes, that is desirable.

What would come from $k = 0 ? not sure, I guess the mantissa would be zero and the exponent could be technically anything. As a caveat it would be desirable for the exponent to be zero for the purposes of further calculation.

If $k was defined to be 8.980e9, would you expect the mantissa to be 8.980? Yes.

If $k were defined to be 8980000000 and the intent was that any number of those 0s were significant, would you still expect to see 8.98 as a mantissa? I suppose the natural option would be to reduce to standard form where the mantissa was normalised to be between 1 and 10. However, in certain circumstances (I can't think of any specific examples right now) it may be advantageous to parse the number to an arbitrary mantissa and exponent specified by the user.

Hope this answers you questions.
Zak
In reply to Zak Zarychta

Re: extracting mantissa and exponent from float values?

by Danny Glin -
The following should do the basics of what you want.  It would need to be fixed to handle negative values, or to handle negative exponents properly, but it should give you a starting point:
$exp_k = int(ln($a)/ln(10));
$mant_k = $a/10**$exp_a;