-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add opaque types for Double and Float
- Loading branch information
1 parent
98bbc62
commit 20c49db
Showing
4 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import stainless.math._ | ||
|
||
object FPFledging { | ||
def test1(x: Double): Unit = { | ||
assert(Double.longBitsToDouble(Double.doubleToRawLongBits(x)) == x) | ||
} | ||
|
||
def test2(x: Long): Unit = { | ||
Double.longBitsToDoublePost(x) | ||
assert(Double.doubleToRawLongBits(Double.longBitsToDouble(x)) == x) | ||
} | ||
|
||
def test3(x: Float): Unit = { | ||
assert(Float.intBitsToFloat(Float.floatToRawIntBits(x)) == x) | ||
} | ||
|
||
def test4(x: Int): Unit = { | ||
Float.intBitsToFloatPost(x) | ||
assert(Float.floatToRawIntBits(Float.intBitsToFloat(x)) == x) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package stainless | ||
package math | ||
|
||
import lang.StaticChecks._ | ||
import annotation._ | ||
|
||
@library | ||
case class Double(@extern @pure private val value: scala.Double) extends AnyVal | ||
|
||
@library | ||
object Double { | ||
@extern @pure | ||
def longBitsToDouble(l: Long): Double = { | ||
Double(java.lang.Double.longBitsToDouble(l)) | ||
} | ||
|
||
@extern @pure | ||
def longBitsToDoublePost(l: Long): Unit = { | ||
() | ||
} ensuring (_ => doubleToRawLongBits(longBitsToDouble(l)) == l) | ||
|
||
@extern @pure | ||
def doubleToRawLongBits(d: Double): Long = { | ||
java.lang.Double.doubleToRawLongBits(d.value) | ||
} ensuring (res => longBitsToDouble(res) == d) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package stainless | ||
package math | ||
|
||
import lang.StaticChecks._ | ||
import annotation._ | ||
|
||
@library | ||
case class Float(@extern @pure private val value: scala.Float) extends AnyVal | ||
|
||
@library | ||
object Float { | ||
@extern @pure | ||
def intBitsToFloat(i: Int): Float = { | ||
Float(java.lang.Float.intBitsToFloat(i)) | ||
} | ||
|
||
@extern @pure | ||
def intBitsToFloatPost(i: Int): Unit = { | ||
() | ||
} ensuring (_ => floatToRawIntBits(intBitsToFloat(i)) == i) | ||
|
||
@extern @pure | ||
def floatToRawIntBits(d: Float): Int = { | ||
java.lang.Float.floatToRawIntBits(d.value) | ||
} ensuring (res => intBitsToFloat(res) == d) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters