1

I was trying to get my head around how invalid mathematical values are handled in Rust, so I thought I will have a look at what happens if I take the tangent of 90 degrees. What I didn't expect is that I will get a 'proper' value so when I run this code:

fn main() {
    use std::f32;

    for i in 89997..90003 {
        let angle = (i as f32) / 1000.0;
        println!("tan of {}: {}", angle, angle.to_radians().tan());
    }
}

I get the following result:

tan of 89.997: 19124.42
tan of 89.998: 28665.936
tan of 89.999: 57208.06
tan of 90: -22877334
tan of 90.001: -57312.285
tan of 90.002: -28692.08

What is the meaning of -22877334 in this context?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
dawid
  • 665
  • 2
  • 6
  • 12
  • 3
    `90` may be a value which can be precisely represented, but `angle.to_radians()` is pi/2, which cannot. Clearly you expect tan(pi/2) to be infinity, but actually it's calculating tan(pi/2 * x) where x is some value very close to one. Try `180.to_radians().tan()` and you'll see what I mean. – Wai Ha Lee Jan 09 '18 at 23:26

0 Answers0