3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-06-23 17:10:33 +00:00
z3/src/api/java/RatNum.java
Angelica Moreira bebad7da50
Add numeral extraction helpers to Java API (#8978)
New methods:
- Expr.getNumeralDouble(): retrieve any numeral as a double
- IntNum.getUint(): extract numeral as unsigned 32-bit value
- IntNum.getUint64(): extract numeral as unsigned 64-bit value
- RatNum.getSmall(): numerator/denominator as int64 pair
- RatNum.getRationalInt64(): numerator/denominator (returns null on overflow)

Each is a thin wrapper around the existing Native binding.
Added examples to JavaExample.java covering all new methods.
2026-03-15 10:36:17 -07:00

114 lines
2.8 KiB
Java

/**
Copyright (c) 2012-2014 Microsoft Corporation
Module Name:
RatNum.java
Abstract:
Author:
@author Christoph Wintersteiger (cwinter) 2012-03-15
Notes:
**/
package com.microsoft.z3;
import java.math.BigInteger;
/**
* Rational Numerals
**/
public class RatNum extends RealExpr
{
/**
* The numerator of a rational numeral.
**/
public IntNum getNumerator()
{
return new IntNum(getContext(), Native.getNumerator(getContext().nCtx(),
getNativeObject()));
}
/**
* The denominator of a rational numeral.
**/
public IntNum getDenominator()
{
return new IntNum(getContext(), Native.getDenominator(getContext().nCtx(),
getNativeObject()));
}
/**
* Converts the numerator of the rational to a BigInteger
**/
public BigInteger getBigIntNumerator()
{
IntNum n = getNumerator();
return new BigInteger(n.toString());
}
/**
* Converts the denominator of the rational to a BigInteger
**/
public BigInteger getBigIntDenominator()
{
IntNum n = getDenominator();
return new BigInteger(n.toString());
}
/**
* Retrieve the numerator and denominator as 64-bit integers.
* Throws if the value does not fit in 64-bit integers.
* @return a two-element array [numerator, denominator]
**/
public long[] getSmall()
{
Native.LongPtr num = new Native.LongPtr();
Native.LongPtr den = new Native.LongPtr();
if (!Native.getNumeralSmall(getContext().nCtx(), getNativeObject(), num, den))
throw new Z3Exception("Numeral does not fit in int64");
return new long[] { num.value, den.value };
}
/**
* Retrieve the numerator and denominator as 64-bit integers.
* Returns null if the value does not fit in 64-bit integers.
* @return a two-element array [numerator, denominator], or null
**/
public long[] getRationalInt64()
{
Native.LongPtr num = new Native.LongPtr();
Native.LongPtr den = new Native.LongPtr();
if (!Native.getNumeralRationalInt64(getContext().nCtx(), getNativeObject(), num, den))
return null;
return new long[] { num.value, den.value };
}
/**
* Returns a string representation in decimal notation.
* Remarks: The result
* has at most {@code precision} decimal places.
**/
public String toDecimalString(int precision)
{
return Native.getNumeralDecimalString(getContext().nCtx(), getNativeObject(),
precision);
}
/**
* Returns a string representation of the numeral.
**/
@Override
public String toString() {
return Native.getNumeralString(getContext().nCtx(), getNativeObject());
}
RatNum(Context ctx, long obj)
{
super(ctx, obj);
}
}