package interval_base

  1. Overview
  2. Docs

Base module for interval arithmetic.

  • version 1.5.1
module type T = sig ... end

Basic signature for interval arithmetic packages.

Intervals with float endpoints

type t = {
  1. low : float;
    (*

    lower bound, possibly = -∞

    *)
  2. high : float;
    (*

    higher bound, possibly = +∞

    *)
}

The interval type. Be careful however when creating intervals. For example, the following code: let a = {low=1./.3.; high=1./.3.} creates an interval which does NOT contain the mathematical object 1/3.

If you want to create an interval representing 1/3, you have to write let a = I.(inv(v 3. 3.)) because rounding will then be properly handled by I.inv and the resulting interval will indeed contain the exact value of 1/3.

exception Division_by_zero

Exception raised when a division by 0 occurs.

exception Domain_error of string

Exception raised when an interval is completely outside the domain of a function. The string is the name of the function and is meant to help when running code in the REPL (aka toploop).

module I : sig ... end

Interval operations. Locally open this module — using e.g. I.(...) — to redefine classical arithmetic operators for interval arithmetic.

Directed rounding

module type DIRECTED = sig ... end

Interface for up and down roundings.

module Low : sig ... end

Functions rounding down their results.

module High : sig ... end

Functions rounding up their results.

Changing the rounding mode (DANGEROUS)

Below, we have functions for changing the rounding mode. The default mode for rounding is NEAREST.

BE VERY CAREFUL: using these functions unwisely can ruin all your computations. Remember also that on 64 bits machine these functions won't change the behaviour of the SSE instructions.

When setting the rounding mode to UPWARD or DOWNWARD, it is better to set it immediately back to NEAREST. However we have no guarantee on how the compiler will reorder the instructions generated. It is ALWAYS better to write:

let a = set_high(); let res = 1./.3. in
set_nearest (); res;; 

The above code will NOT work on linux-x64 where many floating point functions are implemented using SSE instructions. These three functions should only be used when there is no other solution, and you really know what tou are doing, and this should never happen. Please use the regular functions of the fpu module for computations. For example prefer:

let a = High.(1. /. 3.)  

PS: The Interval module and the fpu module functions correctly set and restore the rounding mode for all interval computations, so you don't really need these functions.

PPS: Please, don't use them...

val set_low : unit -> unit

Sets the rounding mod to DOWNWARD (towards minus infinity)

val set_high : unit -> unit

Sets the rounding mod to UPWARD (towards infinity)

val set_nearest : unit -> unit

Sets the rounding mod to NEAREST (default mode)

OCaml

Innovation. Community. Security.