Rounding errors

SYS 64738
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Rounding errors

Post by Shaun_B »

Here's something I noticed which I assume is due to rounding errors:

Code: Select all

10 let c = 0
20 for i = 0 to 1 step 0.01
30 print c; ":"; i; "  ";
40 let c = c+1
50 next i
Note what happens from 0.79 onwards (and some numbers beforehand I think).

Is there an easy way to resolve this, or would one simply find a different way to do the same thing (as I did in my last example)?

Regards,

Shaun.


BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
ajordison
Developer
Developer
Posts: 59
Joined: Wed Apr 16, 2014 1:28 pm
Location: Hartlepool, UK
Contact:

Re: Rounding errors

Post by ajordison »

Well if you don't care about the value of i until you see it, try:

Code: Select all

10 let c = 0
20 for i = 0 to 100 step 1
30 print c; ":"; i/100; "  ";
40 let c = c+1
50 next i
Try out CBM prg Studio for all your Commodore development needs!
Stay a while...stay for erm quite a while!
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Rounding errors

Post by Shaun_B »

That is what I ended up doing :-)

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
satpro

Re: Rounding errors

Post by satpro »

Shaun, are you talking about the high # of decimal places after you pass 79?
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Rounding errors

Post by Shaun_B »

Yeah it seems that CBM BASIC can't add 0.01 in all instances, or a bug in the FOR/STEP/NEXT combination when step is used for decimal points.

I'd already solved the problem in the same way that ajordison has suggested, and have thought of another solution using something else that ajordison had suggested in another thread (ie, using the str$ keyword to convert number to string).

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Rounding errors

Post by Shaun_B »

I haven't bothered cleaning the solution here, but this is the other solution that I thought up to get around the rounding problems:

Code: Select all

0 dim i$(100):i$(0)="0.00":i$(100)="1.00":c=1
1 for i=0.01 to 1.00 step 0.0100009999
2 i$(c)="0"+mid$(str$(i),2,3):c=c+1
3 next i
4 for i=0 to 100:print i$(i):next i
Takes a little while to populate the array, and of course if you wanted to use the numeric values, you'd have to use the val() keyword.

I don't know if there are any benefits other than strings being easier to deal with when printing.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
dudz
Member
Member
Posts: 140
Joined: Tue Jun 17, 2014 5:40 am
Contact:

Re: Rounding errors

Post by dudz »

one should NEVER (read: NEVER) use floating point variables as loop counters, because what is shown in this thread will happen. always use integers for the loop counter - and count up the floating point variable seperately in the loop (or calculate it by other means) if you must. its a fundamental problem and exists in all programming languages, its not a basic v2 problem.
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Rounding errors

Post by Shaun_B »

Thanks Dudz,

I've read up on some of this, ie http://floating-point-gui.de/ as I noticed that PHP and JavaScript were producing slightly different results on using division. It was from this that I realised that using multiplication was probably a better way to work out percentages as it seemed to produce more consistent results across technologies.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 4 guests