| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- ADA.NUMERICS.GENERIC_ELEMENTARY_FUNCTIONS -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2012-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- This specification is derived from the Ada Reference Manual for use with -- |
| 12 |
-- GNAT. The copyright notice above, and the license provisions that follow -- |
| 13 |
-- apply solely to the Post aspects that have been added to the spec. -- |
| 14 |
-- -- |
| 15 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 16 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 17 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 18 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 19 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 20 |
-- or FITNESS FOR A PARTICULAR PURPOSE. -- |
| 21 |
-- -- |
| 22 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 23 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 24 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 25 |
-- -- |
| 26 |
-- You should have received a copy of the GNU General Public License and -- |
| 27 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 28 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 29 |
-- <http://www.gnu.org/licenses/>. -- |
| 30 |
-- -- |
| 31 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 32 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 33 |
-- -- |
| 34 |
------------------------------------------------------------------------------ |
| 35 |
|
| 36 |
generic |
| 37 |
type Float_Type is digits <>; |
| 38 |
|
| 39 |
package Ada.Numerics.Generic_Elementary_Functions with |
| 40 |
SPARK_Mode => On, |
| 41 |
Always_Terminates |
| 42 |
is |
| 43 |
pragma Pure; |
| 44 |
|
| 45 |
-- Preconditions in this unit are meant for analysis only, not for run-time |
| 46 |
-- checking, so that the expected exceptions are raised when calling |
| 47 |
-- Assert. This is enforced by setting the corresponding assertion policy |
| 48 |
-- to Ignore. This is done in the generic spec so that it applies to all |
| 49 |
-- instances. |
| 50 |
|
| 51 |
pragma Assertion_Policy (Pre => Ignore); |
| 52 |
|
| 53 |
function Sqrt (X : Float_Type'Base) return Float_Type'Base with |
| 54 |
Global => null, |
| 55 |
Pre => X >= 0.0, |
| 56 |
Post => Sqrt'Result >= 0.0 |
| 57 |
and then (if X = 0.0 then Sqrt'Result = 0.0) |
| 58 |
and then (if X = 1.0 then Sqrt'Result = 1.0) |
| 59 |
|
| 60 |
-- Finally if X is positive, the result of Sqrt is positive (because |
| 61 |
-- the sqrt of numbers greater than 1 is greater than or equal to 1, |
| 62 |
-- and the sqrt of numbers less than 1 is greater than the argument). |
| 63 |
|
| 64 |
-- This property is useful in particular for static analysis. The |
| 65 |
-- property that X is positive is not expressed as (X > 0.0), as |
| 66 |
-- the value X may be held in registers that have larger range and |
| 67 |
-- precision on some architecture (for example, on x86 using x387 |
| 68 |
-- FPU, as opposed to SSE2). So, it might be possible for X to be |
| 69 |
-- 2.0**(-5000) or so, which could cause the number to compare as |
| 70 |
-- greater than 0, but Sqrt would still return a zero result. |
| 71 |
|
| 72 |
-- Note: we use the comparison with Succ (0.0) here because this is |
| 73 |
-- more amenable to CodePeer analysis than the use of 'Machine. |
| 74 |
|
| 75 |
and then (if X >= Float_Type'Succ (0.0) then Sqrt'Result > 0.0); |
| 76 |
|
| 77 |
function Log (X : Float_Type'Base) return Float_Type'Base with |
| 78 |
Global => null, |
| 79 |
Pre => X > 0.0, |
| 80 |
Post => (if X = 1.0 then Log'Result = 0.0); |
| 81 |
|
| 82 |
function Log (X, Base : Float_Type'Base) return Float_Type'Base with |
| 83 |
Global => null, |
| 84 |
Pre => X > 0.0 and Base > 0.0 and Base /= 1.0, |
| 85 |
Post => (if X = 1.0 then Log'Result = 0.0); |
| 86 |
|
| 87 |
function Exp (X : Float_Type'Base) return Float_Type'Base with |
| 88 |
Global => null, |
| 89 |
Post => (if X = 0.0 then Exp'Result = 1.0); |
| 90 |
|
| 91 |
function "**" (Left, Right : Float_Type'Base) return Float_Type'Base with |
| 92 |
Global => null, |
| 93 |
Pre => (if Left = 0.0 then Right > 0.0) and Left >= 0.0, |
| 94 |
Post => "**"'Result >= 0.0 |
| 95 |
and then (if Right = 0.0 then "**"'Result = 1.0) |
| 96 |
and then (if Right = 1.0 then "**"'Result = Left) |
| 97 |
and then (if Left = 1.0 then "**"'Result = 1.0) |
| 98 |
and then (if Left = 0.0 then "**"'Result = 0.0); |
| 99 |
|
| 100 |
function Sin (X : Float_Type'Base) return Float_Type'Base with |
| 101 |
Inline, |
| 102 |
Global => null, |
| 103 |
Post => Sin'Result in -1.0 .. 1.0 |
| 104 |
and then (if X = 0.0 then Sin'Result = 0.0); |
| 105 |
|
| 106 |
function Sin (X, Cycle : Float_Type'Base) return Float_Type'Base with |
| 107 |
Global => null, |
| 108 |
Pre => Cycle > 0.0, |
| 109 |
Post => Sin'Result in -1.0 .. 1.0 |
| 110 |
and then (if X = 0.0 then Sin'Result = 0.0); |
| 111 |
|
| 112 |
function Cos (X : Float_Type'Base) return Float_Type'Base with |
| 113 |
Inline, |
| 114 |
Global => null, |
| 115 |
Post => Cos'Result in -1.0 .. 1.0 |
| 116 |
and then (if X = 0.0 then Cos'Result = 1.0); |
| 117 |
|
| 118 |
function Cos (X, Cycle : Float_Type'Base) return Float_Type'Base with |
| 119 |
Global => null, |
| 120 |
Pre => Cycle > 0.0, |
| 121 |
Post => Cos'Result in -1.0 .. 1.0 |
| 122 |
and then (if X = 0.0 then Cos'Result = 1.0); |
| 123 |
|
| 124 |
function Tan (X : Float_Type'Base) return Float_Type'Base with |
| 125 |
Global => null, |
| 126 |
Post => (if X = 0.0 then Tan'Result = 0.0); |
| 127 |
|
| 128 |
function Tan (X, Cycle : Float_Type'Base) return Float_Type'Base with |
| 129 |
Global => null, |
| 130 |
Pre => Cycle > 0.0 |
| 131 |
and then abs Float_Type'Base'Remainder (X, Cycle) /= 0.25 * Cycle, |
| 132 |
Post => (if X = 0.0 then Tan'Result = 0.0); |
| 133 |
|
| 134 |
function Cot (X : Float_Type'Base) return Float_Type'Base with |
| 135 |
Global => null, |
| 136 |
Pre => X /= 0.0; |
| 137 |
|
| 138 |
function Cot (X, Cycle : Float_Type'Base) return Float_Type'Base with |
| 139 |
Global => null, |
| 140 |
Pre => Cycle > 0.0 |
| 141 |
and then X /= 0.0 |
| 142 |
and then Float_Type'Base'Remainder (X, Cycle) /= 0.0 |
| 143 |
and then abs Float_Type'Base'Remainder (X, Cycle) /= 0.5 * Cycle; |
| 144 |
|
| 145 |
function Arcsin (X : Float_Type'Base) return Float_Type'Base with |
| 146 |
Global => null, |
| 147 |
Pre => abs X <= 1.0, |
| 148 |
Post => (if X = 0.0 then Arcsin'Result = 0.0); |
| 149 |
|
| 150 |
function Arcsin (X, Cycle : Float_Type'Base) return Float_Type'Base with |
| 151 |
Global => null, |
| 152 |
Pre => Cycle > 0.0 and abs X <= 1.0, |
| 153 |
Post => (if X = 0.0 then Arcsin'Result = 0.0); |
| 154 |
|
| 155 |
function Arccos (X : Float_Type'Base) return Float_Type'Base with |
| 156 |
Global => null, |
| 157 |
Pre => abs X <= 1.0, |
| 158 |
Post => (if X = 1.0 then Arccos'Result = 0.0); |
| 159 |
|
| 160 |
function Arccos (X, Cycle : Float_Type'Base) return Float_Type'Base with |
| 161 |
Global => null, |
| 162 |
Pre => Cycle > 0.0 and abs X <= 1.0, |
| 163 |
Post => (if X = 1.0 then Arccos'Result = 0.0); |
| 164 |
|
| 165 |
function Arctan |
| 166 |
(Y : Float_Type'Base; |
| 167 |
X : Float_Type'Base := 1.0) return Float_Type'Base |
| 168 |
with |
| 169 |
Global => null, |
| 170 |
Pre => X /= 0.0 or Y /= 0.0, |
| 171 |
Post => (if X > 0.0 and then Y = 0.0 then Arctan'Result = 0.0); |
| 172 |
|
| 173 |
function Arctan |
| 174 |
(Y : Float_Type'Base; |
| 175 |
X : Float_Type'Base := 1.0; |
| 176 |
Cycle : Float_Type'Base) return Float_Type'Base |
| 177 |
with |
| 178 |
Global => null, |
| 179 |
Pre => Cycle > 0.0 and (X /= 0.0 or Y /= 0.0), |
| 180 |
Post => (if X > 0.0 and then Y = 0.0 then Arctan'Result = 0.0); |
| 181 |
|
| 182 |
function Arccot |
| 183 |
(X : Float_Type'Base; |
| 184 |
Y : Float_Type'Base := 1.0) return Float_Type'Base |
| 185 |
with |
| 186 |
Global => null, |
| 187 |
Pre => X /= 0.0 or Y /= 0.0, |
| 188 |
Post => (if X > 0.0 and then Y = 0.0 then Arccot'Result = 0.0); |
| 189 |
|
| 190 |
function Arccot |
| 191 |
(X : Float_Type'Base; |
| 192 |
Y : Float_Type'Base := 1.0; |
| 193 |
Cycle : Float_Type'Base) return Float_Type'Base |
| 194 |
with |
| 195 |
Global => null, |
| 196 |
Pre => Cycle > 0.0 and (X /= 0.0 or Y /= 0.0), |
| 197 |
Post => (if X > 0.0 and then Y = 0.0 then Arccot'Result = 0.0); |
| 198 |
|
| 199 |
function Sinh (X : Float_Type'Base) return Float_Type'Base with |
| 200 |
Post => (if X = 0.0 then Sinh'Result = 0.0); |
| 201 |
|
| 202 |
function Cosh (X : Float_Type'Base) return Float_Type'Base with |
| 203 |
Global => null, |
| 204 |
Post => Cosh'Result >= 1.0 |
| 205 |
and then (if X = 0.0 then Cosh'Result = 1.0); |
| 206 |
|
| 207 |
function Tanh (X : Float_Type'Base) return Float_Type'Base with |
| 208 |
Global => null, |
| 209 |
Post => Tanh'Result in -1.0 .. 1.0 |
| 210 |
and then (if X = 0.0 then Tanh'Result = 0.0); |
| 211 |
|
| 212 |
function Coth (X : Float_Type'Base) return Float_Type'Base with |
| 213 |
Global => null, |
| 214 |
Pre => X /= 0.0, |
| 215 |
Post => abs Coth'Result >= 1.0; |
| 216 |
|
| 217 |
function Arcsinh (X : Float_Type'Base) return Float_Type'Base with |
| 218 |
Global => null, |
| 219 |
Post => (if X = 0.0 then Arcsinh'Result = 0.0); |
| 220 |
|
| 221 |
function Arccosh (X : Float_Type'Base) return Float_Type'Base with |
| 222 |
Global => null, |
| 223 |
Pre => X >= 1.0, |
| 224 |
Post => Arccosh'Result >= 0.0 |
| 225 |
and then (if X = 1.0 then Arccosh'Result = 0.0); |
| 226 |
|
| 227 |
function Arctanh (X : Float_Type'Base) return Float_Type'Base with |
| 228 |
Global => null, |
| 229 |
Pre => abs X < 1.0, |
| 230 |
Post => (if X = 0.0 then Arctanh'Result = 0.0); |
| 231 |
|
| 232 |
function Arccoth (X : Float_Type'Base) return Float_Type'Base with |
| 233 |
Global => null, |
| 234 |
Pre => abs X > 1.0; |
| 235 |
|
| 236 |
end Ada.Numerics.Generic_Elementary_Functions; |