| 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 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::f32rust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/; |
| 26 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::f64rust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/; |
| 27 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#; |
| 28 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#; |
| 29 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Divrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#; |
| 30 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#; |
| 31 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Negrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#; |
| 32 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/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 FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType#: |
| 37 | Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# |
| 38 | + Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy# |
| 39 | + Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# |
| 40 | + Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<i16> |
| 41 | + PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq# |
| 42 | + PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd# |
| 43 | + Negrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#Output# = Self> |
| 44 | + Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#Output# = Self> |
| 45 | + Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#Output# = Self> |
| 46 | + Mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#Output# = Self> |
| 47 | + Divrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#Output# = Self> |
| 48 | { |
| 49 | fn zerorust-analyzer cargo roots 0.0.8 float/FloatType#zero().() -> Self; |
| 50 | fn onerust-analyzer cargo roots 0.0.8 float/FloatType#one().() -> Self; |
| 51 | fn one_thirdrust-analyzer cargo roots 0.0.8 float/FloatType#one_third().() -> Self; |
| 52 | fn pirust-analyzer cargo roots 0.0.8 float/FloatType#pi().() -> Self; |
| 53 | fn two_third_pirust-analyzer cargo roots 0.0.8 float/FloatType#two_third_pi().() -> Self; |
| 54 | fn sqrtrust-analyzer cargo roots 0.0.8 float/FloatType#sqrt().(self) -> Self; |
| 55 | /// The cubic root function is pow(x, 1/3) accepting negative arguments |
| 56 | fn cbrtrust-analyzer cargo roots 0.0.8 float/FloatType#cbrt().(self) -> Self { |
| 57 | if self rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().Self::zerorust-analyzer cargo roots 0.0.8 float/FloatType#zero().() { |
| 58 | -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#neg().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#neg().self).powfrust-analyzer cargo roots 0.0.8 float/FloatType#powf().(Self::one_thirdrust-analyzer cargo roots 0.0.8 float/FloatType#one_third().()) |
| 59 | } else { |
| 60 | self.powfrust-analyzer cargo roots 0.0.8 float/FloatType#powf().(Self::one_thirdrust-analyzer cargo roots 0.0.8 float/FloatType#one_third().()) |
| 61 | } |
| 62 | } |
| 63 | fn atanrust-analyzer cargo roots 0.0.8 float/FloatType#atan().(self) -> Self; |
| 64 | fn acosrust-analyzer cargo roots 0.0.8 float/FloatType#acos().(self) -> Self; |
| 65 | fn sinrust-analyzer cargo roots 0.0.8 float/FloatType#sin().(self) -> Self; |
| 66 | fn cosrust-analyzer cargo roots 0.0.8 float/FloatType#cos().(self) -> Self; |
| 67 | fn absrust-analyzer cargo roots 0.0.8 float/FloatType#abs().(self) -> Self; |
| 68 | fn powfrust-analyzer cargo roots 0.0.8 float/FloatType#powf().(self, n: Self) -> Self; |
| 69 | } |
| 70 | |
| 71 | impl FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType# for f32 { |
| 72 | #[inline] |
| 73 | fn zerorust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType]zero().() -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType] { |
| 74 | 0f32 |
| 75 | } |
| 76 | #[inline] |
| 77 | fn one_thirdrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType]one_third().() -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType] { |
| 78 | 1f32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().3f32 |
| 79 | } |
| 80 | #[inline] |
| 81 | fn onerust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType]one().() -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType] { |
| 82 | 1f32 |
| 83 | } |
| 84 | #[inline] |
| 85 | fn two_third_pirust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType]two_third_pi().() -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType] { |
| 86 | 2f32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().f32rust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/::constsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/consts/::FRAC_PI_3rust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/consts/FRAC_PI_3. |
| 87 | } |
| 88 | #[inline] |
| 89 | fn pirust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType]pi().() -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType] { |
| 90 | f32rust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/::constsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/consts/::PIrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/consts/PI. |
| 91 | } |
| 92 | fn sqrtrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType]sqrt().(self) -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType] { |
| 93 | self.sqrtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]sqrt().() |
| 94 | } |
| 95 | fn atanrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType]atan().(self) -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType] { |
| 96 | self.atanrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]atan().() |
| 97 | } |
| 98 | fn acosrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType]acos().(self) -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType] { |
| 99 | self.acosrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]acos().() |
| 100 | } |
| 101 | fn sinrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType]sin().(self) -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType] { |
| 102 | self.sinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]sin().() |
| 103 | } |
| 104 | fn cosrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType]cos().(self) -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType] { |
| 105 | self.cosrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]cos().() |
| 106 | } |
| 107 | fn absrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType]abs().(self) -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType] { |
| 108 | self.absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]abs().() |
| 109 | } |
| 110 | fn powfrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType]powf().(self, n: Selfrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType]) -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f32][FloatType] { |
| 111 | self.powfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]powf().(n) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | impl FloatTyperust-analyzer cargo roots 0.0.8 float/FloatType# for f64 { |
| 116 | #[inline] |
| 117 | fn zerorust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType]zero().() -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType] { |
| 118 | 0f64 |
| 119 | } |
| 120 | #[inline] |
| 121 | fn one_thirdrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType]one_third().() -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType] { |
| 122 | 1f64 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().3f64 |
| 123 | } |
| 124 | #[inline] |
| 125 | fn onerust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType]one().() -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType] { |
| 126 | 1f64 |
| 127 | } |
| 128 | #[inline] |
| 129 | fn two_third_pirust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType]two_third_pi().() -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType] { |
| 130 | 2f64 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().f64rust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/::constsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/consts/::FRAC_PI_3rust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/consts/FRAC_PI_3. |
| 131 | } |
| 132 | #[inline] |
| 133 | fn pirust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType]pi().() -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType] { |
| 134 | f64rust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/::constsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/consts/::PIrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/consts/PI. |
| 135 | } |
| 136 | fn sqrtrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType]sqrt().(self) -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType] { |
| 137 | self.sqrtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]sqrt().() |
| 138 | } |
| 139 | fn atanrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType]atan().(self) -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType] { |
| 140 | self.atanrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]atan().() |
| 141 | } |
| 142 | fn acosrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType]acos().(self) -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType] { |
| 143 | self.acosrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]acos().() |
| 144 | } |
| 145 | fn sinrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType]sin().(self) -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType] { |
| 146 | self.sinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]sin().() |
| 147 | } |
| 148 | fn cosrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType]cos().(self) -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType] { |
| 149 | self.cosrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]cos().() |
| 150 | } |
| 151 | fn absrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType]abs().(self) -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType] { |
| 152 | self.absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]abs().() |
| 153 | } |
| 154 | fn powfrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType]powf().(self, n: Selfrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType]) -> Selfrust-analyzer cargo roots 0.0.8 float/impl#[f64][FloatType] { |
| 155 | self.powfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]powf().(n) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 160 | fn test_float_cbrtrust-analyzer cargo roots 0.0.8 float/test_float_cbrt().() { |
| 161 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().8f64.cbrtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]cbrt().(), -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().2f64); |
| 162 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(8f64.cbrtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]cbrt().(), 2f64); |
| 163 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(0f32.cbrtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]cbrt().(), 0f32); |
| 164 | } |