Tcl Source Code

Artifact [8d3d8a8ad5]
Login

Artifact 8d3d8a8ad54a44e87db1a901a3084c2023192825:

Attachment "isqrt.c" to ticket [2143288fff] added by [email protected] 2008-10-03 06:35:26. Also attachment "isqrt.c" to ticket [2143232fff] added by [email protected] 2008-10-03 05:55:55. Also attachment "isqrt.c" to ticket [2143217fff] added by [email protected] 2008-10-03 05:43:44.
// Show effect of optimisation of fp results.
// Code taken from ~/src/tcl/tcl8.6a2/libtommath/bn_mp_sqrt.c
// Shows bug in 'if (d != 0.0)', with d being inexact
// Compile and run with -
//   make CC=/usr/local/gcc-4.2.1/bin/gcc CFLAGS=-O2 LDFLAGS=-lm isqrt && ./isqrt
//
#include <math.h>
#include <stdio.h>
int main()
{
    double d = 0.0;
    int i;
    unsigned long dig;
    for (i = 0; i < 2; i++) {
	d = ldexp(d, 28) + (double)((1<<28)-1);
	//printf("d=%g=%a\n", d, d);
    }
    d=sqrt(d);
    dig = (unsigned long) ldexp(d, -28);
    printf("d=%g=%a dig=%d\n", d, d, dig);
    d -= ldexp((double) dig, 28);
    printf("d=%g=%a dig=%d\n", d, d, dig);
}