| 1 | // Copyright (c) 2015, Mikhail Vorotilov |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // * Redistributions of source code must retain the above copyright notice, this |
| 8 | // list of conditions and the following disclaimer. |
| 9 | // |
| 10 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 18 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 20 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 21 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 22 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | |
| 25 | use std::f32; |
| 26 | use std::f64; |
| 27 | use std::fmt::Debug; |
| 28 | use std::ops::Add; |
| 29 | use std::ops::Div; |
| 30 | use std::ops::Mul; |
| 31 | use std::ops::Neg; |
| 32 | use std::ops::Sub; |
| 33 | |
| 34 | /// Generic type that lists functions and constants needed in calculations. |
| 35 | /// Default implementations for f32 and f64 are provided. |
| 36 | pub trait FloatType: |
| 37 | Sized |
| 38 | + Copy |
| 39 | + Debug |
| 40 | + From<i16> |
| 41 | + PartialEq |
| 42 | + PartialOrd |
| 43 | + Neg<Output = Self> |
| 44 | + Add<Output = Self> |
| 45 | + Sub<Output = Self> |
| 46 | + Mul<Output = Self> |
| 47 | + Div<Output = Self> |
| 48 | { |
| 49 | fn zero() -> Self; |
| 50 | fn one() -> Self; |
| 51 | fn one_third() -> Self; |
| 52 | fn pi() -> Self; |
| 53 | fn two_third_pi() -> Self; |
| 54 | fn sqrt(self) -> Self; |
| 55 | /// The cubic root function is pow(x, 1/3) accepting negative arguments |
| 56 | fn cbrt(self) -> Self { |
| 57 | if self < Self::zero() { |
| 58 | -(-self).powf(Self::one_third()) |
| 59 | } else { |
| 60 | self.powf(Self::one_third()) |
| 61 | } |
| 62 | } |
| 63 | fn atan(self) -> Self; |
| 64 | fn acos(self) -> Self; |
| 65 | fn sin(self) -> Self; |
| 66 | fn cos(self) -> Self; |
| 67 | fn abs(self) -> Self; |
| 68 | fn powf(self, n: Self) -> Self; |
| 69 | } |
| 70 | |
| 71 | impl FloatType for f32 { |
| 72 | #[inline] |
| 73 | fn zero() -> Self { |
| 74 | 0f32 |
| 75 | } |
| 76 | #[inline] |
| 77 | fn one_third() -> Self { |
| 78 | 1f32 / 3f32 |
| 79 | } |
| 80 | #[inline] |
| 81 | fn one() -> Self { |
| 82 | 1f32 |
| 83 | } |
| 84 | #[inline] |
| 85 | fn two_third_pi() -> Self { |
| 86 | 2f32 * f32::consts::FRAC_PI_3 |
| 87 | } |
| 88 | #[inline] |
| 89 | fn pi() -> Self { |
| 90 | f32::consts::PI |
| 91 | } |
| 92 | fn sqrt(self) -> Self { |
| 93 | self.sqrt() |
| 94 | } |
| 95 | fn atan(self) -> Self { |
| 96 | self.atan() |
| 97 | } |
| 98 | fn acos(self) -> Self { |
| 99 | self.acos() |
| 100 | } |
| 101 | fn sin(self) -> Self { |
| 102 | self.sin() |
| 103 | } |
| 104 | fn cos(self) -> Self { |
| 105 | self.cos() |
| 106 | } |
| 107 | fn abs(self) -> Self { |
| 108 | self.abs() |
| 109 | } |
| 110 | fn powf(self, n: Self) -> Self { |
| 111 | self.powf(n) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | impl FloatType for f64 { |
| 116 | #[inline] |
| 117 | fn zero() -> Self { |
| 118 | 0f64 |
| 119 | } |
| 120 | #[inline] |
| 121 | fn one_third() -> Self { |
| 122 | 1f64 / 3f64 |
| 123 | } |
| 124 | #[inline] |
| 125 | fn one() -> Self { |
| 126 | 1f64 |
| 127 | } |
| 128 | #[inline] |
| 129 | fn two_third_pi() -> Self { |
| 130 | 2f64 * f64::consts::FRAC_PI_3 |
| 131 | } |
| 132 | #[inline] |
| 133 | fn pi() -> Self { |
| 134 | f64::consts::PI |
| 135 | } |
| 136 | fn sqrt(self) -> Self { |
| 137 | self.sqrt() |
| 138 | } |
| 139 | fn atan(self) -> Self { |
| 140 | self.atan() |
| 141 | } |
| 142 | fn acos(self) -> Self { |
| 143 | self.acos() |
| 144 | } |
| 145 | fn sin(self) -> Self { |
| 146 | self.sin() |
| 147 | } |
| 148 | fn cos(self) -> Self { |
| 149 | self.cos() |
| 150 | } |
| 151 | fn abs(self) -> Self { |
| 152 | self.abs() |
| 153 | } |
| 154 | fn powf(self, n: Self) -> Self { |
| 155 | self.powf(n) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn test_float_cbrt() { |
| 161 | assert_eq!(-8f64.cbrt(), -2f64); |
| 162 | assert_eq!(8f64.cbrt(), 2f64); |
| 163 | assert_eq!(0f32.cbrt(), 0f32); |
| 164 | } |