30 Desember 2018

Interval arithmetic with mpfi

Interval arithmetic is a mature field in computer science that was conceived in the 50s or 60s. Mostly it was used to ensure the precision of floating point numbers, i.e. a floating point number's possible max and min value fall into certain precision. E.g. 3.13< pi< 3.15

Problem: having an f(x), we want to find the max and min of f(x) for x between x1 and x2.

I'm trying to solve this problem with mpfi library:
  • libmpfi-dev - multiple precision floating-point interval computation library
  • libmpfi0 - multiple precision floating-point interval computation library

Here is the code for f(x)=x^3 with x1=-4 and x2=3:
 
#include "stdio.h"
#include "mpfi.h"
#include "mpfi_io.h"

int main(int argc, char** argv) {
  mpfi_t x, fx;

  mpfi_init(x);
  mpfi_init(fx);
  mpfi_interv_si(x, -4, 3);

  //fx = x*x*x
  mpfi_mul(fx, x, x); //fx = x*x
  mpfi_mul(fx, fx, x); //fx = fx*x
  mpfi_out_str(stdout, 10, 2, fx); printf("\n");
  mpfi_clear(x);
  mpfi_clear(fx);
  return 0;
}
Compilation:
 $ gcc -lmpfi test.c
$ ./a.out
[-6.4e1,4.8e1]
The result is min f(x)= -64 and max f(x)= 48 for x1=-4 and x2=3

Tidak ada komentar: