Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: cos (100000000.0)

by kavehmz (Initiate)
on Sep 01, 2009 at 14:56 UTC ( [id://792679]=note: print w/replies, xml ) Need Help??


in reply to Re: cos (100000000.0)
in thread cos (100000000.0)

Maybe you answer is different from mine because you are running Perl in another architecture. Are you using a Intel based machine?
And how can I decide, up to what point I can trust Perl calculations?

Becasue it is a different situation that we have intrinsic error in floating point numbers like
http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

For that kind of expected problems i am ready to check my values properly.

But for what I get from a call to a language function why should it be careful that it will return a wrong number?

Is there a way that I restrict Perl return values to what is safe for big number without truncating all return numbers from native Perl functions by myself?
And when Perl is returning numbers in this fashion can I be sure that if I use bigger numbers, the error wont grow to make more wrong digits?

Replies are listed 'Best First'.
Re^3: cos (100000000.0)
by ikegami (Patriarch) on Sep 01, 2009 at 15:31 UTC

    Maybe you answer is different from mine because you are running Perl in another architecture. Are you using a Intel based machine?

    I've already said it's the underlying C library that makes the difference for cos().

    And how can I decide, up to what point I can trust Perl calculations?

    It's my understanding that libraries often approximate the result of trig functions. In Perl's case, it uses the underlying C library. I don't know what kind of precision guarantees (if any) C makes concerning its trig functions. Feel free to look it up.

    And when Perl is returning numbers in this fashion can I be sure that if I use bigger numbers, the error wont grow to make more wrong digits?

    I don't understand the question. You single out the previously discussed manner or returning numbers, but no ways of returning numbers were previously discussed. What does Perl returning numbers even mean? What are the other manners in which Perl returns numbers to which you allude?

    Is there a way that I restrict Perl return values to what is safe for big number without truncating all return numbers from native Perl functions by myself?

    The premise makes no sense. Truncating and rounding reduce precision.

    Actual: -0.3633850893556905538... Java's error: 0.0000000000000000538... Your C's error: 0.0000000000001194462... Rounded error: 0.0000000000003094462... Truncated error: 0.0000000000006905538...

    Besides that, the precision of the result of each operation depends on the precision of the result of each operand of that operation. Neither Perl nor the computer know the precision of the operands, so it can't know the precision of the output.

Re^3: cos (100000000.0)
by syphilis (Archbishop) on Sep 01, 2009 at 23:29 UTC
    Maybe you answer is different from mine because you are running Perl in another architecture.

    I am running perl in a different architecture (amd64), and I get the same result as you for perl:
    C:\_32>perl -e "print cos(100000000.0)" -0.36338508935581
    I'm on Win32, and that's using the Microsoft C runtime library (msvcrt.dll). If I instead use the mpfr library, then I get the correct result:
    C:\_32>perl -MMath::MPFR -e "$x=Math::MPFR->new(100000000.0);print cos +($x)" -3.6338508935569053e-1
    Cheers,
    Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://792679]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found