## MOD

Expand Messages
• This question has arisen from the Odd number thread. How come the following code: *********************************** struct qaz, a as double, b as double, c
Message 1 of 9 , Aug 1, 2007
This question has arisen from the Odd number thread.

How come the following code:

'***********************************
struct qaz, a as double, b as double, c as double, d as double, e as
double

a=1.9999999999
b=2.9999999999
c=2.5
d=3

a= a mod 2
b= b mod 2
c= c mod 2
d= d mod 2

print a,b,c,d
'************************************

produces the following as an output?

2.0 1.0 0.5 1

Thanks

Nick
• ... Most BASIC dialects convert both arguments to integers before performing the MOD operation, and in that case MOD 2 can only ever return the values zero
Message 2 of 9 , Aug 1, 2007
--- In libertybasic@yahoogroups.com, "Nick Fraser" wrote:
> How come the following code:
> [snip]
> produces the following as an output?
>
> 2.0 1.0 0.5 1

Most BASIC dialects convert both arguments to integers before
performing the MOD operation, and in that case 'MOD 2' can only ever
return the values zero (0) and one (1). That's certainly true of
the other BASICs I've tried (BASICA, QBasic, BBC BASIC).

However strictly speaking you can define MOD for non-integer
arguments and it would appear that Liberty Basic does so.
Therefore, using the definition that MOD returns the remainder after
a division, the results you are seeing are to be expected (allowing
for the fact that 1.9999999999 is rounded up to 2.0 for display).

So, LB is unusual in working that way, but arguably not incorrect.

Richard.
• I ve been watching the Odd Number thread and now the MOD thread for a while now and I m posting now to as people to drop both topics. Here s a story.. Many
Message 3 of 9 , Aug 1, 2007
I've been watching the Odd Number thread and now the MOD thread for a while now and I'm posting now to as people to drop both topics.

Here's a story..
Many years ago (1981), I was selling electronics, including computers. Although I knew the product pretty well I did not know anything about the internal workings of the operating system or the details of representing decimal numbers using binary arithmetic. A person kept coming into the store and tapping away at the display computer to write a small program. This is long before the internet and thoughts of malicious code etc. His program was specifically designed to show that when doing apparently simple arithmetic the computer would make tiny errors. His software was set up to amplify those errors and the end result was that the computer claimed that 2 multiplied by 3 gave a result of 5.9999999998 or something. He seemed very proud to have discovered our computer to be so faulty and announced that computers in general were flawed and not to be relied upon. He suggested that when the tax office sent us a computer generated assessment of our tax bill we should show them his program and declare loudly that binary computers did not understand decimal mathematics and so on.

The problem was, when told of this, the tax office would declare the speaker to be a nut.

The irony is...the tax office was right.

We should all accept that there are inherent problems trying to describe the infinity of numbers in a universal format. Try representing one third as a decimal. You will soon run out of paper (or computer memory) as you write an endless list of 3333333333's. so at some stage you will say "enough is enough...I have described this number to so many decimal places that I'm close enough for all practical purposes." Then take the result you decided on and multiply it by 3. Logic tells you that it should equal 1 but reality shows you it will be .9999999999999999...9999999" and so on depending on where you decided "enough is enough"

Nobody here has discovered any "bug" in Liberty Basic. They have simply re-discovered the problems that arise when you try to represent the infinity of numbers in the finite space of binary logic when limited by a finite size to represent each number. I guess if I looked hard enough I could discover that when my car Speedo said I was doing 60Mph I was actually doing 59.9998763624. So what. I don't care about this tiny level of error. It will not get me off that speeding fine I got last week. The tax office does not care if their assessment is inaccurate by .000000000001 cents. They just want me to pay my tax bill.

I accept that some people can write a program specifically to expose the errors produced when binary numbers are used to represent numbers in the real world. Congratulations to them...they have performed a significant academic exercise. But let's not confuse their ability to write these programs with their ability to expose any real-world implications of these errors.

----- Original Message -----
From: Nick Fraser
To: libertybasic@yahoogroups.com
Sent: Wednesday, August 01, 2007 6:48 PM
Subject: [libertybasic] MOD

This question has arisen from the Odd number thread.

How come the following code:

'***********************************
struct qaz, a as double, b as double, c as double, d as double, e as
double

a=1.9999999999
b=2.9999999999
c=2.5
d=3

a= a mod 2
b= b mod 2
c= c mod 2
d= d mod 2

print a,b,c,d
'************************************

produces the following as an output?

2.0 1.0 0.5 1

Thanks

Nick

[Non-text portions of this message have been removed]
• ... You re right in nearly every respect, but it is worth adding that when it comes to financial calculations small errors may accumulate and eventually give
Message 4 of 9 , Aug 2, 2007
--- In libertybasic@yahoogroups.com, "Angus Mann" wrote:
> The tax office does not care if their assessment is inaccurate
> by .000000000001 cents. They just want me to pay my tax bill.

You're right in nearly every respect, but it is worth adding that when
it comes to financial calculations small errors may accumulate and
eventually give rise to differences which really do matter (if
a 'balance sheet' is out by as little as \$0.01 it is likely to be
rejected).

So, specifically when working with financial quantities, you should
always do your calculations in integer numbers of cents (pence etc.)
and divide by 100 when you want to display dollars (or pounds etc.).
That way no errors, however small, should arise.

Richard.
• Checking out the sudoku.bas program (uploaded by akron_phillies a year ago)(I have been massaging one of own for a year or so), I immediately came across the
Message 5 of 9 , Oct 9, 2008
Checking out the "sudoku.bas" program (uploaded by akron_phillies a
year ago)(I have been massaging one of own for a year or so), I
immediately came across the command "MOD."

Modulo arithmetic is very handy, and whenever I have used it in a
program I have always had to write my own routine or function because
MOD IS NOWHERE LISTED IN THE LIBERTY BASIC HELP FILES!

(To the best of my knowledge and belief... I made a mistake once, I
suppose it could happen again?)

What's up with that, Carl? Are there any more handy commands hidden in
LB? :-)

...from the world according to GaRPMorE
• From the Helpfile: num1 MOD num2 Description: The MOD command returns the remainder when num1 is divided by num2. Usage: divide 15 by 4 and return the
Message 6 of 9 , Oct 9, 2008
From the Helpfile:

num1 MOD num2

Description:
The MOD command returns the remainder when num1 is divided by num2.

Usage:

'divide 15 by 4 and return the remainder
print 15 MOD 4
print 27 MOD 16

'produces
3
11

When 15 is divided by 4, the quotient is 3, with a remainder of 3. MOD returns that remainder. When 27 is divided by 16, the quotient is 1, with a remainder of 11. MOD returns that remainder.

[Non-text portions of this message have been removed]
• I know what it is. I didn t know LB had it. I searched both the help files from LB 4.03 (w/Assist) and the PDF doc I have to use on this Vista machine.. Mod
Message 7 of 9 , Oct 9, 2008
I know what it is. I didn't know LB had it. I searched both the help files from LB 4.03 (w/Assist) and the PDF doc I have to use on this Vista machine.. "Mod" never came up. Of course it only takes a few characters to code it "from scratch," but that does fall under the category of "Inventing the Wheel Again."

Just to satisfy my curiosity, Bill, can you telll me the edition and page number where you found that? It's only been eight months since I got the Liberty 4 version.

I'd like be just a "bit" smarter.

Gary, languishing in GaRPMorE's world.

[Non-text portions of this message have been removed]
• I obtained the information directly from LB s helpfiles, simply by typing in mod . Of course, since you are using a VISTA machine (poor fellow, but I have
Message 8 of 9 , Oct 9, 2008
I obtained the information directly from LB's helpfiles, simply by typing in "mod". Of course, since you are using a VISTA machine (poor fellow, but I have one also), you need to go to the General Board > LB on Vista, and check out Stefan's link to download WinHelp32 in comment #1. I have JB and LB both on my Vista machine, and everything works just fine! Note also, on Vista, LB should be installed in it's own folder directly under C:/ and not under C:/Program Files.

Good luck to you!

[Non-text portions of this message have been removed]
• ... Liberty BASIC 4.03 Help File: 1) Contents = What s New = What s new in LB 4.02 2) Contents = Language Syntax and Usage = Mathematics = Mathematics =
Message 9 of 9 , Oct 9, 2008
>
> Just to satisfy my curiosity, Bill, can you telll me the
> edition and page number where you found that? It's only been
> eight months since I got the Liberty 4 version.
>

Liberty BASIC 4.03 Help File:

1) Contents => What's New => What's new in LB 4.02
2) Contents => Language Syntax and Usage =>