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