Skip to content

Commit

Permalink
Add math package (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnezam authored Oct 17, 2023
1 parent 4e1b00e commit 7e7975e
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions math.gobra
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// This package defines mathematical operations commonly used in specifications.
package math

ghost
decreases
pure func Max(x, y int) int {
return x > y ? x : y
}

ghost
decreases
pure func Max3(x, y, z int) int {
return Max(Max(x, y), z)
}

ghost
decreases
pure func Min(x, y int) int {
return x < y ? x : y
}

ghost
decreases
pure func Min3(x, y, z int) int {
return Min(Min(x, y), z)
}

ghost
decreases
pure func Abs(x int) int {
return x >= 0 ? x : -x
}

// Pow raises x to the power of e.
ghost
requires e >= 0
decreases e
pure func Pow(x, e int) int {
return e == 0 ? 1 : x * Pow(x, e - 1)
}

// Pow raises 2 to the power of e.
ghost
requires e >= 0
decreases
pure func Pow2(e int) int {
return Pow(2, e)
}

// Clips x to the interval [low, high].
// If x is not in the interval, Clip returns the closest
// value in the interval. Otherwise, it returns x.
ghost
requires low <= high
decreases
pure func Clip(x, low, high int) int {
return Min(Max(x, low), high)
}

0 comments on commit 7e7975e

Please sign in to comment.