root / pobysoPythonSage / src / sageSLZ / sageNumericalOperations.sage @ 281
Historique | Voir | Annoter | Télécharger (1,03 ko)
| 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 | 268 | storres | As of Sage 7.1, the built-in is_power_of_two, only works with integer and |
| 7 | 172 | storres | returns false for negative ones. |
| 8 | 172 | storres | """ |
| 9 | 268 | storres | #return is_power_of_two(abs(number)) |
| 10 | 268 | storres | log2number = log(abs(number))/log(2) |
| 11 | 268 | storres | return log2number.n() == long(log2number.n()) |
| 12 | 172 | storres | # End sno_abs_is_power_of_two(number): |
| 13 | 246 | storres | # |
| 14 | 179 | storres | def sno_float_to_rat_pow_of_two_denom(number): |
| 15 | 179 | storres | """ |
| 16 | 179 | storres | Convert a floating-point number into a rational where the denominator |
| 17 | 179 | storres | is a power of two |
| 18 | 179 | storres | """ |
| 19 | 179 | storres | ## Argument checking. |
| 20 | 179 | storres | if not number.is_real(): |
| 21 | 179 | storres | return None |
| 22 | 179 | storres | # |
| 23 | 179 | storres | (sign, mantissa, exponent) = number.sign_mantissa_exponent() |
| 24 | 179 | storres | #print sign, mantissa, exponent |
| 25 | 179 | storres | precision = number.prec() |
| 26 | 179 | storres | if sign == 1: |
| 27 | 187 | storres | return mantissa / 2^(-exponent) |
| 28 | 179 | storres | else: |
| 29 | 187 | storres | return -mantissa / 2^(-exponent) |
| 30 | 246 | storres | # End sno_float_to_rat_pow_of_two_denom. |
| 31 | 179 | storres | |
| 32 | 246 | storres | sys.stderr.write("\t...sageNumericalOperations loaded.\n") |