JML Tutorial - JML's \real built-in mathematical real type
\real is the type of mathematical real numbers. Mathematical reals are easier for solvers to reason about than floating point numbers, so using them is natural
in writing specifications.
Since \real is a JML built-in type, it may only be used within specifications. But that includes using it as the type of ghost fields, ghost variables
and the
parameters and result types of model methods.
The \real built-in type is intuitive and natural to use, but for the sake of clarity, its operations are listed here. j and k are two values of
type \real.
j == kandj != kmean equality and inequality- jis mathematical real negationj + kis mathematical real additionj - kis mathematical real subtractionj * kis mathematical real multiplicationj / kis mathematical real division (not rounded to an integer);kmust not be 0j % kis mathematical real modulo, but is similar to Java’s%operation:kmay not be zero,j%khas the same sign asjand is independent of the sign ofk, and fork != 0,((\bigint)(j/k))*k + (j%k) == j.<and<=and>and>=have their expected meanings with the result type being boolean- implicit conversions are allowed from
double,real,\bigintand Java integral types - explicit casts are allowed to and from other numeric types, such as
(\bigint)j, which truncates towards zero. - also, the static method
\real.of(n)converts its argument to a\realvalue, just like an explicit cast.ncan be any Java numeric value, aBigIntegeror aBigDecimal - similarly the methods
r.intValue,r.bigintValue()and so on for other numeric types convert a\realrto the destination type
! Caveat: Though real numbers are supported in OpenJML, the connection between real numbers and floating point numbers is incomplete and in some cases (such as handling NaN and infinite fp numbers) wrong
\real can be used like this:
//@ ghost \real r;
//@ ghost \real rr;
//@ set rr = r * 2;
Just like for \bigint, many of the methods in java.lang.Math have been specified for real values:
// openjml --esc Real.java
public class Real {
//@ ghost \real x;
//@ ghost \real y;
public void test() {
//@ assert Math.abs(x) >= 0;
//@ assert Math.max(x,y) >= Math.min(x,y);
//@ assert Math.sin(x) >= -1;
}
}