use std::cmp::Ordering;

/// Compares 2 f64s and returns an std::cmp::Ordering.
///
/// # Examples
///
/// ```rust
/// use cmp_floats::cmp_floats;
///
/// assert_eq!(std::cmp::Ordering::Less, cmp_floats(2.71828, 3.14159));
/// ```
pub fn cmp_floats(f1: f64, f2: f64) -> Ordering {
    let f1 = f1.to_string();
    let f1_dot = f1.chars().position(|x| x == '.').unwrap();
    let f1 = (&f1[0..f1_dot], &f1[f1_dot + 1..f1.len()]);

    let f2 = f2.to_string();
    let f2_dot = f2.chars().position(|x| x == '.').unwrap();
    let f2 = (&f2[0..f2_dot], &f2[f2_dot + 1..f2.len()]);

    let f1_int = f1.0.parse::<i32>().unwrap();
    let f2_int = f2.0.parse::<i32>().unwrap();

    if f1_int > f2_int {
        return std::cmp::Ordering::Greater;
    } else if f1_int < f2_int {
        return std::cmp::Ordering::Less;
    }
    for (ch1, ch2) in f1.1.chars().zip(f2.1.chars()) {
        let n1 = ch1.to_digit(10);
        let n2 = ch2.to_digit(10);
        if let (Some(n1), Some(n2)) = (n1, n2) {
            if n1 > n2 { return std::cmp::Ordering::Greater }
            if n1 < n2 { return std::cmp::Ordering::Less }
        }
    }
    std::cmp::Ordering::Equal
}
