Help! Perl fails op.int test

Paul Sutcliffe Jr. paul at devon.UUCP
Thu Nov 24 03:38:48 AEST 1988


I've recently ported PERL to a Tandy 6000 (MC68000 CPU, Xenix/68k 3.0).
Perl passes every test in the t/ directory *except* op.int, which tests
the use of perl's int() function.  All four tests in op.int fail.  It
appears that the crux of the problem is that "int(1.234)" does not
return 1.000000 exactly.  The following will illustrate this:

    $ perl -e "print int(1.234);"
    1.0000002407468855
    $

I dug into perl's code a little and found that the int() function
uses modf() to get the integer portion (base) of it's argument.  So I
tried this little test:

    #include <stdio.h>

    main()
    {
	double value = 1.234;

	printf("before modf: %lf\n", value);
	modf(value, &value);
	printf("after modf:  %lf\n", value);
    }

which, when run, yields:

    $ a.out
    before modf: 1.234000
    after modf:  1.000000
    $

Looks correct enough.  However, changing both "%lf"'s to "%.16lf"
produces:

    $ a.out
    before modf: 1.2339999999999999
    after modf:  1.0000002407468855
    $

Where have I seen that value (after modf) before!?  I guess that what
I'm seeing is error introduced into the values by conversion from float
to double (or the other way around).

In short, what can I do to get perl's int() function to behave?
Or am I (quoting the man page) "at the mercy of [my] machine's
definitions of various operations such as type casting, atof() and
sprintf()."

Please answer via email, I'll summarize to the net.

- paul

-- 
Paul Sutcliffe, Jr.			  +---------------------------------+
					  | Light Year, n.:  A regular year |
UUCP: paul at devon.UUCP			  |   that has 1/3 less calories.   |
 or : ...rutgers!bpa!vu-vlsi!devon!paul	  +---------------------------------+



More information about the Comp.sources.bugs mailing list