// Copyright 2026 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
let sign_mask : UInt64 = 0x8000_0000_0000_0000

///|
let exp_bias = 1023

///|
let exp_bits = 11

///|
let frac_bits = 52

///|
/// Returns an integer value by discarding the decimal part of the floating-point
/// number (truncation toward zero).
///
/// Parameters:
///
/// * `self` : The floating-point number to be truncated.
///
/// Returns a floating-point number representing the integer part of the input.
///
/// Example:
///
/// ```mbt check
/// test {
///   inspect(3.7.trunc(), content="3")
///   inspect((-3.7).trunc(), content="-3")
///   inspect(0.0.trunc(), content="0")
/// }
/// ```
pub fn Double::trunc(self : Double) -> Double {
  let u64 = self.reinterpret_as_uint64()
  let biased_exp = ((u64 >> frac_bits) & ((0x1UL << exp_bits) - 1)).to_int()
  if biased_exp < exp_bias {
    return (u64 & sign_mask).reinterpret_as_double()
  } else if biased_exp >= exp_bias + frac_bits {
    return self
  }
  let mask_shift = biased_exp - exp_bias + exp_bits
  let trunc_mask = (sign_mask.reinterpret_as_int64() >> mask_shift).reinterpret_as_uint64()
  return (u64 & trunc_mask).reinterpret_as_double()
}

///|
/// Returns the smallest integer greater than or equal to the given number.
///
/// Parameters:
///
/// * `self` : The floating point number to find the ceiling of.
///
/// Returns the ceiling value of the input number.
///
/// Example:
///
/// ```mbt check
/// test {
///   inspect(3.7.ceil(), content="4")
///   inspect((-3.7).ceil(), content="-3")
///   inspect(42.0.ceil(), content="42")
/// }
/// ```
pub fn Double::ceil(self : Double) -> Double {
  let trunced = self.trunc()
  if self > trunced {
    return trunced + 1.0
  } else {
    return trunced
  }
}

///|
/// Returns the largest integer less than or equal to the given number.
///
/// Parameters:
///
/// * `number` : A floating-point number to be rounded down.
///
/// Returns a double-precision floating-point number representing the largest
/// integer less than or equal to the input.
///
/// Example:
///
/// ```mbt check
/// test {
///   inspect(3.7.floor(), content="3")
///   inspect((-3.7).floor(), content="-4")
///   inspect(0.0.floor(), content="0")
/// }
/// ```
pub fn Double::floor(self : Double) -> Double {
  let trunced = self.trunc()
  if self < trunced {
    return trunced - 1.0
  } else {
    return trunced
  }
}

///|
/// Rounds a floating-point number to the nearest integer using "round half up"
/// rule. In this rule, when a number is halfway between two integers (like 3.5),
/// it is rounded up to the next integer.
///
/// Parameters:
///
/// * `value` : The floating-point number to be rounded.
///
/// Returns the rounded value as a double-precision floating-point number.
///
/// Example:
///
/// ```mbt check
/// test {
///   inspect(3.7.round(), content="4")
///   inspect(3.2.round(), content="3")
///   inspect(3.5.round(), content="4")
///   inspect((-3.5).round(), content="-3")
/// }
/// ```
pub fn Double::round(self : Double) -> Double {
  // `(self + 0.5).floor()` alone drops the sign of zero, crosses the half
  // boundary for `nextDown(0.5)`, and perturbs integers in `[2^52, 2^53)`.
  if self == 0.0 || self != self || self.abs() >= 4503599627370496.0 {
    return self
  }
  if self > 0.0 {
    if self < 0.5 {
      0.0
    } else {
      (self + 0.5).floor()
    }
  } else if self >= -0.5 {
    -0.0
  } else {
    (self + 0.5).floor()
  }
}