about summary refs log tree commit diff
path: root/src/libstd/f32.rs
AgeCommit message (Collapse)AuthorLines
2020-07-27mv std libs to library/mark-1674/+0
2020-07-09libstd/libcore: fix various typosAndy Russell-1/+1
2020-06-19Rollup merge of #72486 - Ralith:asinh-fix, r=dtolnayRalf Jung-5/+3
Fix asinh of negative values Rust's current implementation of asinh has [large errors](https://www.wolframalpha.com/input/?i=arcsinh%28x%29%2C+ln%28x%2B%28x%5E2%2B1%29%5E0.5%29%2C+x+from+-67452095.07139316+to+0) in its negative range. ~These are (mostly) not numerical, but rather seem due to an incorrect implementation.~ This appears to be due to avoidable catastrophic cancellation. [Playground before/after](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=bd04ae6d86d06612e4e389a8b95d19ab). [glibc uses](https://github.com/bminor/glibc/blob/81dca813cc35f91414731fdd0ff6b756d5e1827f/sysdeps/ieee754/dbl-64/s_asinh.c#L56) abs here. Many thanks to @danieldeankon for finding this weird behavior, @jebrosen for diagnosing it, and @toasteater for identifying the probable implementation error!
2020-06-18Remove now-redundant branchBenjamin Saunders-5/+1
2020-06-10Migrate to numeric associated constsLzu Tao-29/+28
2020-05-29Rollup merge of #72568 - golddranks:add_total_cmp_to_floats, r=sfacklerDylan DPC-0/+143
Implement total_cmp for f32, f64 # Overview * Implements method `total_cmp` on `f32` and `f64`. This method implements a float comparison that, unlike the standard `partial_cmp`, is total (defined on all values) in accordance to the IEEE 754 (rev 2008) §5.10 `totalOrder` predicate. * The method has an API similar to `cmp`: `pub fn total_cmp(&self, other: &Self) -> crate::cmp::Ordering { ... }`. * Implements tests. * Has documentation. # Justification for the API * Total ordering for `f32` and `f64` has been discussed many time before: * https://internals.rust-lang.org/t/pre-pre-rfc-range-restricting-wrappers-for-floating-point-types/6701 * https://github.com/rust-lang/rfcs/issues/1249 * https://github.com/rust-lang/rust/pull/53938 * https://github.com/rust-lang/rust/issues/5585 * The lack of total ordering leads to frequent complaints, especially from people new to Rust. * This is an ergonomics issue that needs to be addressed. * However, the default behaviour of implementing only `PartialOrd` is intentional, as relaxing it might lead to correctness issues. * Most earlier implementations and discussions have been focusing on a wrapper type that implements trait `Ord`. Such a wrapper type is, however not easy to add because of the large API surface added. * As a minimal step that hopefully proves uncontroversial, we can implement a stand-alone method `total_cmp` on floating point types. * I expect adding such methods should be uncontroversial because... * Similar methods on `f32` and `f64` would be warranted even in case stdlib would provide a wrapper type that implements `Ord` some day. * It implements functionality that is standardised. (IEEE 754, 2008 rev. §5.10 Note, that the 2019 revision relaxes the ordering. The way we do ordering in this method conforms to the stricter 2008 standard.) * With stdlib APIs such as `slice::sort_by` and `slice::binary_search_by` that allow users to provide a custom ordering criterion, providing additional helper methods is a minimal way of adding ordering functionality. * Not also does it allow easily using aforementioned APIs, it also provides an easy and well-tested primitive for the users and library authors to implement an `Ord`-implementing wrapper, if needed.
2020-05-25Add total_cmp to f32 and f64, plus testsPyry Kontio-0/+143
2020-05-24Fix asinh of negative valuesBenjamin Saunders-1/+3
When `x` has large magnitude, `x + ((x * x) + 1.0).sqrt()` approaches `x + x.abs()`. For negative values of `x`, this leads to catastrophic cancellation, resulting in large errors or even 0 being passed to `ln`, producing incorrect results including `-inf`. Becuase asinh is an odd function, i.e. -asinh(x) = asinh(-x) for all x, we can avoid the catastrophic cancellation and obtain correct results by taking the absolute value of `self` for the first term. `self * self` is always positive, so in effect this gives us `x.abs().asinh().copysign(x)` which as discussed above is algebraically equivalent, but is much more accurate.
2020-05-23Correct small typo: 'not' -> 'note'Jake Goulding-1/+1
2020-04-20Use assoc float consts instead of module levelLinus Färnstrand-4/+4
2020-04-05Remove more std::f32 imports in libstdLinus Färnstrand-39/+15
2020-04-05Stop importing the float modules. Use assoc constsLinus Färnstrand-42/+0
2020-04-03Replace float module consts with assoc consts in documentationLinus Färnstrand-2/+2
2020-02-12Add notice about using new consts in new code on float modulesLinus Färnstrand-0/+3
2020-01-16Rollup merge of #68033 - ollie27:win_f32, r=dtolnayDylan DPC-52/+8
Don't use f64 shims for f32 cmath functions on non 32-bit x86 MSVC These shims are only needed on 32-bit x86. Additionally since https://reviews.llvm.org/rL268875 LLVM handles adding the shims itself for the intrinsics.
2020-01-16Rollup merge of #68266 - Stromberg90:patch-2, r=Dylan-DPCDylan DPC-2/+2
Changed docs for f32 and f64.
2020-01-16Update f32.rsStrømberg-1/+1
2020-01-16Update f32.rsStrømberg-1/+1
2020-01-15Use 3.6 instead of 3.5 in float fract() documentationSOFe-4/+4
It is not self-explanatory whether the fract() function inverts the fractional part of negative numbers. Co-Authored-By: Mateusz Mikuła <mati865@users.noreply.github.com>
2020-01-08Don't use f64 shims for f32 cmath functions on none 32-bit x86 MSVCOliver Middleton-52/+8
These shims are only needed on 32-bit x86. Additionally since https://reviews.llvm.org/rL268875 LLVM handles adding the shims itself for the intrinsics.
2020-01-04Remove negative number check from float sqrtOliver Middleton-1/+1
It hasn't been UB to pass negative numbers to sqrt since https://reviews.llvm.org/D28797 which was included in LLVM 5.
2019-11-29Format libstd with rustfmtDavid Tolnay-45/+37
This commit applies rustfmt with rust-lang/rust's default settings to files in src/libstd *that are not involved in any currently open PR* to minimize merge conflicts. THe list of files involved in open PRs was determined by querying GitHub's GraphQL API with this script: https://gist.github.com/dtolnay/aa9c34993dc051a4f344d1b10e4487e8 With the list of files from the script in outstanding_files, the relevant commands were: $ find src/libstd -name '*.rs' \ | xargs rustfmt --edition=2018 --unstable-features --skip-children $ rg libstd outstanding_files | xargs git checkout -- Repeating this process several months apart should get us coverage of most of the rest of libstd. To confirm no funny business: $ git checkout $THIS_COMMIT^ $ git show --pretty= --name-only $THIS_COMMIT \ | xargs rustfmt --edition=2018 --unstable-features --skip-children $ git diff $THIS_COMMIT # there should be no difference
2019-11-09Auto merge of #63871 - BatmanAoD:FloatFnMustUse, r=withoutboatsbors-4/+42
Add #[must_use] to all functions 'fn(float) -> float' These are pure functions. ```rust impl f32/f64 { fn floor(self) -> Self; fn ceil(self) -> Self; fn round(self) -> Self; fn trunc(self) -> Self; fn fract(self) -> Self; fn abs(self) -> Self; fn signum(self) -> Self; fn mul_add(self, a: Self, b: Self) -> Self; fn div_euclid(self, rhs: Self) -> Self; fn rem_euclid(self, rhs: Self) -> Self; fn powi(self, n: i32) -> Self; fn powf(self, n: Self) -> Self; fn sqrt(self) -> Self; fn exp(self) -> Self; fn exp2(self) -> Self; fn ln(self) -> Self; fn log(self, base: Self) -> Self; fn log2(self) -> Self; fn log10(self) -> Self; fn abs_sub(self, other: Self) -> Self; fn cbrt(self) -> Self; fn hypot(self, other: Self) -> Self; fn sin(self) -> Self; fn cos(self) -> Self; fn tan(self) -> Self; fn asin(self) -> Self; fn acos(self) -> Self; fn atan(self) -> Self; fn atan2(self, other: Self) -> Self; fn exp_m1(self) -> Self; fn ln_1p(self) -> Self; fn sinh(self) -> Self; fn cosh(self) -> Self; fn tanh(self) -> Self; fn asinh(self) -> Self; fn acosh(self) -> Self; fn atanh(self) -> Self; fn clamp(self, min: Self, max: Self) -> Self; } ``` Part of #48926
2019-09-05Restore 'must_use' for 'clamp'; fix warning for testsKyle Strand-5/+4
2019-08-28Let 'clamp' be invoked without using return value; used in testsKyle Strand-1/+2
2019-08-28Annotate functions taking extra argsKyle Strand-1/+11
2019-08-28Add reason for each 'must_use'Kyle Strand-29/+29
2019-08-27Rollup merge of #63698 - Phosphorus15:master, r=nagisaMazdak Farrokhzad-4/+6
Fixed floating point issue with asinh function This should fixes #63271 , in which `asinh(-0.0)` returns `0.0` instead of `-0.0`. according to @nagisa > > > IEEE-754 (2008), section 9.2.1: > > > For the functions expm1, exp2m1, exp10m1, logp1, log2p1, log10p1, sin, tan, sinPi, atanPi, asin, atan, sinh, tanh, asinh, and atanh, f(+0) is +0 and f(−0) is −0 with no exception. > > and > > > sinh(±∞) and asinh(±∞) are ±∞ with no exception. After ensuring that the function `asinh` is the only function affected (functions like `sin`, `sinh` are all based on `cmath` library or `llvm` intrinsics), and that `atanh` always gives the correct result. The only function to modify is `asinh`.
2019-08-24Add 'must_use' to all functions 'fn(float) -> float'Kyle Strand-0/+28
2019-08-22Make use of existing constants.Tomasz Różański-10/+9
f32::consts::PI / 2.0 -> f32::consts::FRAC_PI_2 f32::consts::PI / 4.0 -> f32::consts::FRAC_PI_4 f64::consts::PI / 2.0 -> f64::consts::FRAC_PI_2 f64::consts::PI / 4.0 -> f64::consts::FRAC_PI_4
2019-08-22Change code formatting for readability.Tomasz Różański-9/+9
2019-08-20Refined implementations of `asinh` and `acosh`Phosphorus15-6/+8
2019-08-20Used `copysign` to avoid unnecessary branches.Phosphorus15-9/+1
2019-08-19test cases for both `f32` and `f64` on asinh(-0.0)Phosphorus15-0/+1
2019-08-19Added negative cases for `asinh` according to IEEE-754.Phosphorus15-4/+11
2019-07-07Stablize Euclidean Modulo (feature euclidean_division)CrLF0710-4/+2
2019-06-08Use stable wrappersYuki Okushi-1/+1
2019-04-05Change `copytest` parameter name from `y` to `sign`Will Hakes-5/+5
2019-03-29Stablize {f32,f64}::copysign().CrLF0710-2/+1
2019-03-25Clarify `{Ord,f32,f64}::clamp` docs a littleTobias Bucher-6/+16
Explicitly call out when it returns NaN, adhere to the panic doc guidelines.
2019-03-20Fix formatting and add unit tests for panic casesJacob Kiesel-1/+19
2019-03-20Add NAN test to docsJacob Kiesel-0/+1
2019-03-19Rollup merge of #58812 - jonhoo:floor_v_trunc, r=alexcrichtonMazdak Farrokhzad-4/+8
Clarify distinction between floor() and trunc() `floor()` rounds towards `-INF`, `trunc()` rounds towards 0. This PR clarifies this in the examples.
2019-03-15Auto merge of #58710 - EdorianDark:master, r=sfacklerbors-0/+21
Add clamp for ranges. Implements #44095 Ready for merge
2019-03-09add feature clampDirk Leifeld-0/+1
2019-03-09Revert "Revert "Add clamp functions""Dirk Leifeld-0/+20
2019-02-28Clarify distinction between floor() and trunc()Jon Gjengset-4/+8
2019-02-28libstd => 2018Taiki Endo-8/+8
2018-12-25Remove licensesMark Rousskov-10/+0
2018-12-17rename div_euc -> div_euclid, and mod_euc -> rem_euclidNicole Mazzuca-15/+15
logic is written up in https://github.com/rust-lang/rust/issues/49048 Also, update the documentation slightly