Apache-2.0 WITH LLVM-exception
Version:0.1.0
Alire CI: Dependencies:No dependency.
Dependents:No dependents.
Badge:
A formally verified mathematical library written in SPARK/Ada, providing common mathematical functions for both integer and fixed-point types with compile-time proof of correctness.
Spark_Math.Integer)| Function | Description | Complexity |
|---|---|---|
Sqrt |
Integer square root (floor of sqrt N) | O(log N) |
Power |
Exponentiation with overflow protection | O(Exp) |
GCD |
Greatest common divisor (Euclidean algorithm) | O(log min(A,B)) |
Max |
Maximum of two values | O(1) |
Min |
Minimum of two values | O(1) |
Spark_Math.Fixed)| Function | Description | Complexity |
|---|---|---|
Sqrt |
Square root approximation (within 'Small) |
O(log(N/Small)) |
Max |
Maximum of two values | O(1) |
Min |
Minimum of two values | O(1) |
The goal is to provide SPARK-verified equivalents of Ada.Numerics.Generic_Elementary_Functions for fixed-point and integer types.
| Category | Functions | Status |
|---|---|---|
| Basic | Sqrt |
Implemented |
| Exponential | Log, Log(Base), Exp, ** |
Planned |
| Trigonometric | Sin, Cos, Tan, Cot |
Planned |
| Inverse Trig | Arcsin, Arccos, Arctan, Arccot |
Planned |
| Hyperbolic | Sinh, Cosh, Tanh, Coth |
Planned |
| Inverse Hyperbolic | Arcsinh, Arccosh, Arctanh, Arccoth |
Planned |
Unlike floating-point implementations, fixed-point math requires:
Contributions for any of these functions are welcome!
with Spark_Math.Integer;
procedure Example is
-- Instantiate for your integer type
package My_Math is new Spark_Math.Integer (Integer);
Result : Integer;
begin
-- Square root (floor)
Result := My_Math.Sqrt (17); -- Returns 4
-- Exponentiation
Result := My_Math.Power (2, 10); -- Returns 1024
-- Greatest common divisor
Result := My_Math.GCD (48, 18); -- Returns 6
-- Max and Min
Result := My_Math.Max (5, 3); -- Returns 5
Result := My_Math.Min (5, 3); -- Returns 3
end Example;
with Spark_Math.Fixed;
procedure Example is
-- Define your fixed-point type
type Voltage is delta 0.001 range 0.0 .. 100.0;
-- Instantiate for your type
package Voltage_Math is new Spark_Math.Fixed (Voltage);
Result : Voltage;
begin
-- Square root (approximation within 'Small)
Result := Voltage_Math.Sqrt (4.0); -- Returns ~= 2.0
Result := Voltage_Math.Sqrt (2.0); -- Returns ~= 1.414
-- Max and Min
Result := Voltage_Math.Max (3.5, 2.1); -- Returns 3.5
Result := Voltage_Math.Min (3.5, 2.1); -- Returns 2.1
end Example;
This library is designed for formal verification with SPARK. All functions include:
Pre): Specify valid input constraintsPost): Guarantee output propertiesSPARK defines five proof levels:
| Level | Description |
|---|---|
| Stone | Valid SPARK code |
| Bronze | Initialization and correct data flow |
| Silver | Absence of run-time errors (AoRTE) |
| Gold | Proof of key integrity properties |
| Platinum | Full functional proof of requirements |
| Package | Level |
|---|---|
Spark_Math.Numerics_Core |
Gold |
Spark_Math.Integer |
Gold |
Spark_Math.Fixed |
Gold |
Contributions are welcome!
This project is licensed under the Apache-2.0 WITH LLVM-exception license. See LICENSE for details.