These are chat archives for rust-lang/rust

14th
Apr 2017
Gwen Lofman
@glfmn
Apr 14 2017 01:41
Is there a specific numerical precision associated with PartialEq for the floating-point types?
Ilya Bogdanov
@vitvakatu
Apr 14 2017 06:04
@Lionex standard floating-point types don't have PartialEq trait
Andrey Lesnikov
@ozkriff
Apr 14 2017 06:06

@vitvakatu ?

https://doc.rust-lang.org/std/cmp/trait.PartialEq.html

impl PartialEq<f32> for f32
impl PartialEq<f64> for f64

PartialEq isn't about precision, it's more about equivalence relations: NaN != NaN thing, for example
Eq trait requires a == b and a != b to be strict inverses, but float numbers do not work this way
Gwen Lofman
@glfmn
Apr 14 2017 06:13

Does the == operator actually have a precision though? I noticed that

fn poly_eval(coefficients: &Vec<f64>, x: f64) -> f64 {
    coefficients.iter().fold(0., |b,c| (x*b) + c)
}

 let poly = vec![1.,6.,3.];

assert!(poly_eval(&poly, 0.) == 3.);
assert!(poly_eval(&poly, 1.) == 10.);

Which surprised me; with other functions I've written I had to do something like

assert!((foo(num) - expect).abs() < 1e-12);

so it got me wondering.

Ilya Bogdanov
@vitvakatu
Apr 14 2017 06:17
@ozkriff @Lionex oh, sorry, I confused
Andrey Lesnikov
@ozkriff
Apr 14 2017 06:17
1, 3, 6 and 10 are integers. small integer numbers are represented without any losses in IEEE 754
Gwen Lofman
@glfmn
Apr 14 2017 06:18
That's a cool trick, will definitely use that for more doc tests to make them visually simpler, thanks!
Ilya Bogdanov
@vitvakatu
Apr 14 2017 06:19
Basically, PartialEqimplemented for primitives by this macro:
macro_rules! partial_eq_impl {
        ($($t:ty)*) => ($(
            #[stable(feature = "rust1", since = "1.0.0")]
            impl PartialEq for $t {
                #[inline]
                fn eq(&self, other: &$t) -> bool { (*self) == (*other) }
                #[inline]
                fn ne(&self, other: &$t) -> bool { (*self) != (*other) }
            }
        )*)
    }
Roman Frołow
@rofrol
Apr 14 2017 10:02
Hi
Jonas Platte
@jplatte
Apr 14 2017 10:08
@rofrol Hi!
Roman Frołow
@rofrol
Apr 14 2017 15:28
Not much traffic here
Denis Lisov
@tanriol
Apr 14 2017 15:29
@rofrol The main traffic is on IRC, not here
Ryan
@rnleach
Apr 14 2017 22:15
I have a struct wrapping a SplitWhitespace structure from the std::str module. I'm trying to implement Iterator on my wrapper, where it will parse a few of the tokens from the SplitWhitespace iterator, combine them into a struct and return that. When I call next() on the SplitWhitespace iterator from within my next(&mut self) method, the compiler complains that I " cannot borrow immutable field self.tokens as mutable ". Why is that? I already have a mutable reference to self, why are the contents of self not mutable?
blob
Ryan
@rnleach
Apr 14 2017 22:21
blob
Denis Lisov
@tanriol
Apr 14 2017 22:33

@rnleach Not sure, but I'd try doing

let column_count = self.columns.num_cols();
for i in 0..column_count {

as self.columns.num_cols() borrows self and I'm not sure whether the borrow ends before the actual loop starts.