--  Verada.Vibe.Floats: Python's operations on floats that Ada has no operator for, as CPython
--  computes them (the run time's): `x % y` and `x // y` from the exact remainder, `round(x)`
--  to the nearest int with a half to the even one, `round(x, n)` from the exact value, and
--  `x ** y` with a float exponent.
package Verada.Vibe.Floats is
   --  Python's `x % y`: the remainder with y's sign (0.0 with y's sign when there is none)
   function Modulo (X, Y : Long_Float) return Long_Float
     with Import, Convention => Ada, External_Name => "verada_rt_float_mod",
          Global => null,
          Pre => Y /= 0.0,
          Post => abs Modulo'Result < abs Y
                  and then (if Modulo'Result /= 0.0 then (Modulo'Result < 0.0) = (Y < 0.0));

   --  Python's `x // y`: the floor of x / y, a whole number (the quotient must be a float)
   function Floor_Divide (X, Y : Long_Float) return Long_Float
     with Import, Convention => Ada, External_Name => "verada_rt_float_floor_div",
          Global => null,
          Pre => Y /= 0.0 and then abs (X / Y) <= Long_Float'Last,
          Post => Floor_Divide'Result = Long_Float'Floor (Floor_Divide'Result);

   --  Python's `round(x)`: the nearest int, a half to the even one
   function Round (X : Long_Float) return Integer
     with Import, Convention => Ada, External_Name => "verada_rt_float_round",
          Global => null,
          Pre => X >= -9.2233720368547748E18 and then X <= 9.2233720368547748E18,
          Post => abs (Long_Float (Round'Result) - X) <= 0.5;

   --  Python's `round(x, n)`: x rounded at n digits after the point, a half to the even digit
   function Round_Digits (X : Long_Float; Count : Integer) return Long_Float
     with Import, Convention => Ada, External_Name => "verada_rt_float_round_digits",
          Global => null,
          Post => (if Count >= 0 and then X >= 0.0 then Round_Digits'Result >= 0.0)
                  and then (if Count >= 0 and then X <= 0.0 then Round_Digits'Result <= 0.0);

   --  Python's `x ** y` of floats: a real power (a negative base takes a whole exponent, as
   --  Python's gives a complex number otherwise, and 0.0 a positive one)
   function Power (X, Y : Long_Float) return Long_Float
     with Import, Convention => Ada, External_Name => "verada_rt_float_power",
          Global => null,
          Pre => X > 0.0 or else (X = 0.0 and then Y > 0.0) or else (X < 0.0 and then Y = Long_Float'Floor (Y)),
          Post => (if X > 0.0 then Power'Result >= 0.0);
end Verada.Vibe.Floats;
