#!/usr/local/bin/rune -x # # Test floating point iterations via File.format. Caller should generally # grep for 'Final'. Our increments purposefully do not divide nicely into # a base-2 FP representation, so errors will build up. import "sys"; import ; alias stdio.File *stdout = stdio.stdout; public int main(int ac, char **av) { # The iteration with floats will work but the error is so # large an extra 44 loops will occur. # float_test(); # With doubles it has enough precision and we can get an idea # of the error that built up. # double_test(); # With ldoubles it definitely has enough, and the error is # significantly lower. # ldouble_test(); } void float_test() { float iter; int count; for (iter = 0.0F; iter < 100.0F; iter += 0.001F) { stdout->format("Iteration %7.3f\n", iter); ++count; } stdout->format("Final count=%d fullprec %1.16f\n", count, iter); } void double_test() { double iter; int count; for (iter = 0.0; iter < 100.0; iter += 0.001) { stdout->format("Iteration %7.3f\n", iter); ++count; } stdout->format("Final count=%d fullprec %1.16f\n", count, iter); } void ldouble_test() { ldouble iter; int count; for (iter = 0.0X; iter < 100.0X; iter += 0.001X) { stdout->format("Iteration %7.3f\n", iter); ++count; } stdout->format("Final count=%d fullprec %1.16f\n", count, iter); }