rust/fpnum/src/lib.rs
changeset 15838 c910381d1ea9
parent 15837 42109eb6ef51
equal deleted inserted replaced
15837:42109eb6ef51 15838:c910381d1ea9
   342         } else {
   342         } else {
   343             let sqr: u128 = (self.x_value as u128).pow(2) + (self.y_value as u128).pow(2);
   343             let sqr: u128 = (self.x_value as u128).pow(2) + (self.y_value as u128).pow(2);
   344 
   344 
   345             FPNum {
   345             FPNum {
   346                 sign_mask: POSITIVE_MASK,
   346                 sign_mask: POSITIVE_MASK,
   347                 value: integral_sqrt_ext(sqr) as u64,
   347                 value: integral_sqrt_ext(sqr),
   348             }
   348             }
   349         }
   349         }
   350     }
   350     }
   351 
   351 
   352     #[inline]
   352     #[inline]
   485     }
   485     }
   486 
   486 
   487     result
   487     result
   488 }
   488 }
   489 
   489 
   490 pub fn integral_sqrt_ext(mut value: u128) -> u128 {
   490 pub fn integral_sqrt_ext(value: u128) -> u64 {
   491     let mut digits = (128u32 - 1).saturating_sub(value.leading_zeros()) & 0xFE;
   491     let mut digits = (128u32 - 1).saturating_sub(value.leading_zeros()) & 0xFE;
   492     let mut result = if value == 0 { 0u128 } else { 1u128 };
   492     let mut result = if value == 0 { 0u64 } else { 1u64 };
   493 
   493 
   494     while digits != 0 {
   494     while digits != 0 {
   495         result <<= 1;
   495         result <<= 1;
   496         if (result + 1).pow(2) <= value >> (digits - 2) {
   496         if ((result + 1) as u128).pow(2) <= value >> (digits - 2) {
   497             result += 1;
   497             result += 1;
   498         }
   498         }
   499         digits -= 2;
   499         digits -= 2;
   500     }
   500     }
   501 
   501 
   509 {
   509 {
   510     let sqr: u128 = (x.into().pow(2) as u128).shl(64) + (y.into().pow(2) as u128).shl(64);
   510     let sqr: u128 = (x.into().pow(2) as u128).shl(64) + (y.into().pow(2) as u128).shl(64);
   511 
   511 
   512     FPNum {
   512     FPNum {
   513         sign_mask: POSITIVE_MASK,
   513         sign_mask: POSITIVE_MASK,
   514         value: integral_sqrt_ext(sqr) as u64,
   514         value: integral_sqrt_ext(sqr),
   515     }
   515     }
   516 }
   516 }
   517 
   517 
   518 /* TODO:
   518 /* TODO:
   519  AngleSin
   519  AngleSin
   520  AngleCos
   520  AngleCos
   521 */
   521 */
   522 
   522 
   523 #[cfg(test)]
       
   524 #[test]
   523 #[test]
   525 fn basics() {
   524 fn basics() {
   526     let n = fp!(15 / 2);
   525     let n = fp!(15 / 2);
   527     assert!(n.is_positive());
   526     assert!(n.is_positive());
   528     assert!(!n.is_negative());
   527     assert!(!n.is_negative());
   536 
   535 
   537     assert_eq!(n.round(), 7);
   536     assert_eq!(n.round(), 7);
   538     assert_eq!((-n).round(), -7);
   537     assert_eq!((-n).round(), -7);
   539 
   538 
   540     assert_eq!(f64::from(fp!(5/2)), 2.5f64);
   539     assert_eq!(f64::from(fp!(5/2)), 2.5f64);
       
   540 
       
   541     assert_eq!(integral_sqrt_ext(0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF), 0xFFFF_FFFF_FFFF_FFFF);
   541 }
   542 }
   542 
   543 
   543 #[test]
   544 #[test]
   544 fn zero() {
   545 fn zero() {
   545     let z = fp!(0);
   546     let z = fp!(0);