root / pobysoPythonSage / src / sageSLZ / sageNumericalOperations.sage @ 197
Historique | Voir | Annoter | Télécharger (892 octet)
1 | 172 | storres | print "sageNumericalOperations: loading..." |
---|---|---|---|
2 | 172 | storres | |
3 | 179 | storres | |
4 | 172 | storres | def sno_abs_is_power_of_two(number): |
5 | 172 | storres | """ |
6 | 172 | storres | Check if the absolute value of a number is power of 2. |
7 | 172 | storres | The built-in is_power_of_two, works with many kinds of numbers but |
8 | 172 | storres | returns false for negative ones. |
9 | 172 | storres | """ |
10 | 172 | storres | return is_power_of_two(abs(number)) |
11 | 172 | storres | # End sno_abs_is_power_of_two(number): |
12 | 172 | storres | |
13 | 179 | storres | def sno_float_to_rat_pow_of_two_denom(number): |
14 | 179 | storres | """ |
15 | 179 | storres | Convert a floating-point number into a rational where the denominator |
16 | 179 | storres | is a power of two |
17 | 179 | storres | """ |
18 | 179 | storres | ## Argument checking. |
19 | 179 | storres | if not number.is_real(): |
20 | 179 | storres | return None |
21 | 179 | storres | # |
22 | 179 | storres | (sign, mantissa, exponent) = number.sign_mantissa_exponent() |
23 | 179 | storres | #print sign, mantissa, exponent |
24 | 179 | storres | precision = number.prec() |
25 | 179 | storres | if sign == 1: |
26 | 187 | storres | return mantissa / 2^(-exponent) |
27 | 179 | storres | else: |
28 | 187 | storres | return -mantissa / 2^(-exponent) |
29 | 179 | storres | |
30 | 179 | storres | |
31 | 179 | storres | |
32 | 172 | storres | print "\t...sageNumericalOperations loaded." |