BBC BASIC for Windows
« Subtraction »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 9:53pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Subtraction  (Read 786 times)
JB91
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 47
xx Subtraction
« Thread started on: Feb 16th, 2017, 4:53pm »

Hello,

Can somebody please tell me why the following IF statement does not display the text "Hello!"? I guess it's something to do with the numbers not being integers, but I'm not sure...

Code:
IF 0.3 - 0.2 = 0.1 THEN
PRINT "Hello!"
ENDIF 


Thanks,
Josh
User IP Logged

Zaphod
Guest
xx Re: Subtraction
« Reply #1 on: Feb 16th, 2017, 5:05pm »

Yes, the answer is that computers use floating point calculations so the result is not actually 0.1 but probably something like 0.0999999999998 and therefore fails the test.
Try PRINT 0.3-0.2-0.1
It should be zero but isn't exactly.

Integer arithmetic works, decimal does not exactly but may differ in the last digits that you can't see most of the time.
So you either need to bump up the numbers to be integers or look for the result to be between defined bands that allow for the error.

You can use something like ABS( result -target)< precision needed.
IF ABS(X-0.1)< 0.001
« Last Edit: Feb 16th, 2017, 5:10pm by Zaphod » User IP Logged

JB91
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 47
xx Re: Subtraction
« Reply #2 on: Feb 16th, 2017, 7:39pm »

Thanks Zaphod; it makes sense to me now.

Something like this seems to fix the problem:

Code:
IF FNroundto1dp(0.3 - 0.2) = 0.1 THEN
  PRINT "Hello!"
ENDIF

END

DEF FNroundto1dp(num)
num% = num*10
= num%/10 
User IP Logged

Zaphod
Guest
xx Re: Subtraction
« Reply #3 on: Feb 16th, 2017, 8:53pm »

Now be very careful as when you assign a floating point number to an integer variable then it truncates the decimal part.

If the rounding error in the floating point variable is just over the next integer value it will give a different result to being just under. So your solution may sometimes be off by 0.1

So to truly round you would need to do this:

Code:
DEF FNroundto1dp(num) : =INT(num*10+0.5)/10
  


User IP Logged

JB91
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 47
xx Re: Subtraction
« Reply #4 on: Feb 16th, 2017, 9:17pm »

Quote:
So your solution may sometimes be off by 0.1

Yeah I noticed that...

That rounding technique does looks familiar - I'll remember it from now on!

Thanks for your help,

Josh
User IP Logged

KenDown
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 181
xx Re: Subtraction
« Reply #5 on: Apr 26th, 2017, 1:23pm »

Another way is to convert the numbers to strings.

IFSTR$(0.3-0.2)=STR$(0.1)VDU7

I believe that the conversion process does the necessary rounding up for you.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls