mersenneforum.org  

Go Back   mersenneforum.org > Math Stuff > Computer Science & Computational Number Theory > PARI/GP

Reply
 
Thread Tools
Old 2016-04-20, 19:58   #56
a1call
 
a1call's Avatar
 
"Rashid Naimi"
Oct 2015
Remote to Here/There

24·3·43 Posts
Default

Took me days to pinpoint my script's bug to Pari GP's rounding error:

With deafault precision I get:
Code:
? 100-99.99
%2 = 0.010000000000000000000000000000000000181
I would like to get 0.01 or 0.010000000000000000000000000000000000000
Any advice to resolve it will be greatly appreciated.

Thank you in advance.

Last fiddled with by a1call on 2016-04-20 at 20:04
a1call is online now   Reply With Quote
Old 2016-04-20, 20:24   #57
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

100000110000002 Posts
Default

Quote:
Originally Posted by a1call View Post
Took me days to pinpoint my script's bug to Pari GP's rounding error:

With deafault precision I get:
Code:
? 100-99.99
%2 = 0.010000000000000000000000000000000000181
I would like to get 0.01 or 0.010000000000000000000000000000000000000
Any advice to resolve it will be greatly appreciated.

Thank you in advance.
turn your result into a string at a higher precision using \p precision or localprec and other newer functions before calling it and then use a loop or other means to select the first precision under the precision gp gives you back ? just a thought.
science_man_88 is offline   Reply With Quote
Old 2016-04-20, 20:59   #58
a1call
 
a1call's Avatar
 
"Rashid Naimi"
Oct 2015
Remote to Here/There

24·3·43 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
turn your result into a string at a higher precision using \p precision or localprec and other newer functions before calling it and then use a loop or other means to select the first precision under the precision gp gives you back ? just a thought.
Thank you science_man_88 for the suggestion. I am trying something similar, but i'm hoping for a more general solution whereby I don't have to keep track of the precision.
Wolfram Alpha shows a similar "mouse-over/hover" error, but then gives the correct answer once the enter is pressed.
a1call is online now   Reply With Quote
Old 2016-04-20, 21:10   #59
a1call
 
a1call's Avatar
 
"Rashid Naimi"
Oct 2015
Remote to Here/There

81016 Posts
Default

inconsistent rounding up & down,

makes it much more difficult to deal with:

Code:
? 100-99.999
%2 = 0.00099999999999999999999999999999999994282
a1call is online now   Reply With Quote
Old 2016-04-20, 21:34   #60
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by a1call View Post
inconsistent rounding up & down,

makes it much more difficult to deal with:

Code:
? 100-99.999
%2 = 0.00099999999999999999999999999999999994282
turn both into fractions and you get an exact fraction answer.
science_man_88 is offline   Reply With Quote
Old 2016-04-20, 21:53   #61
a1call
 
a1call's Avatar
 
"Rashid Naimi"
Oct 2015
Remote to Here/There

24·3·43 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
turn both into fractions and you get an exact fraction answer.
I just tried that by multiplying the sum by the fraction numerator, denominator function such that the sum is integers only then devide by denominator. It doesn't seem to work.

What works is truncating the lower digits by rounding, but i have to keep track of significant digits which is inconvenient (figuring out extra code).

Last fiddled with by a1call on 2016-04-20 at 21:54
a1call is online now   Reply With Quote
Old 2016-04-20, 21:59   #62
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

203008 Posts
Default

Quote:
Originally Posted by a1call View Post
I just tried that by multiplying the sum by the fraction numerator, denominator function such that the sum is integers only then devide by denominator. It doesn't seem to work.

What works is truncating the lower digits by rounding, but i have to keep track of significant digits which is inconvenient (figuring out extra code).
try 10000/100 -9999/100 it will work then.
science_man_88 is offline   Reply With Quote
Old 2016-04-20, 22:00   #63
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

2×67×73 Posts
Default

Quote:
Originally Posted by a1call View Post
I just tried that by multiplying the sum by the fraction numerator, denominator function such that the sum is integers only then devide by denominator. It doesn't seem to work.
You're kidding, right?

You don't understand how floating point numbers are represented on digital systems?
chalsall is online now   Reply With Quote
Old 2016-04-20, 22:04   #64
Dubslow
Basketry That Evening!
 
Dubslow's Avatar
 
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88

3·29·83 Posts
Default

a1call, you are definitely in need of reading (e.g.) this page: https://docs.python.org/3.5/tutorial/floatingpoint.html (and possibly sm88 too?)
Dubslow is offline   Reply With Quote
Old 2016-04-20, 22:10   #65
a1call
 
a1call's Avatar
 
"Rashid Naimi"
Oct 2015
Remote to Here/There

24·3·43 Posts
Default

Quote:
Originally Posted by chalsall View Post
You're kidding, right?

You don't understand how floating point numbers are represented on digital systems?
I think I do.

I essentially did this:

100-99.99 =100/100(100-99.99) = (10000-9999)/100

The error seems to be related to the Pari-GP keeping track of the number as much as possible in a/b form and only expanding to decimal point form when needed such as in sums.

Last fiddled with by a1call on 2016-04-20 at 22:12
a1call is online now   Reply With Quote
Old 2016-04-20, 22:17   #66
chalsall
If I May
 
chalsall's Avatar
 
"Chris Halsall"
Sep 2002
Barbados

2·67·73 Posts
Default

Quote:
Originally Posted by a1call View Post
The error seems to be related to the Pari-GP keeping track of the number as much as possible in a/b form and only expanding to decimal point form when needed such as in sums.
Trust the programmer to blame the language.
chalsall is online now   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
An equivalent problem for factorization of large numbers HellGauss Math 5 2012-04-12 14:01
PIV Effective Equivalent curiosities... petrw1 Software 0 2009-12-05 04:41
Dual Core P95 64Bit P4 Equivalent problem g0ods Software 9 2009-09-15 14:12
Is there a finite-field equivalent to the DWT? fivemack Math 4 2008-03-27 17:58
Equivalent code dsouza123 Programming 25 2005-10-08 05:10

All times are UTC. The time now is 23:20.


Fri Aug 6 23:20:22 UTC 2021 up 14 days, 17:49, 1 user, load averages: 4.52, 4.17, 4.08

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.