REPL maths 2

by on under programming
5 minute read

bc, power of zero, PSION programming and REPL

See part 1: Maths, notation, calculators and REPL programming

Modulo/remainders

bc

GNU BC: “modulo” % with scale other than 0

bc -l

scale
20
5 - (5/2) * 2
0
5 - (5/4) * 4
0
5%4
0

scale=0
5 - (5/2) * 2
1
5 - (5/4) * 4
1
5%4
1

Another GNU BC gotcha is the order of precedence aka order of operations is different to C. See comments here: https://www.gnu.org/software/bc/manual/html_node/bc_13.html

That is one reason to use parentheses in large equations to make it more readable and avoid the need to recall many different orders of precedence.

C#

C# Remainder operator % integer and floating point

I was doing some maths in C# for fun which turned out to be “Multinomial distribution” (TODO: more on this in another post) and as I was running multiple iterations with larger and larger numbers I recalled about a nice C# feature for making large literal numbers easier to read.

int i = 100000000;
int n = 100_000_000;

Also see: What do the underscores mean in a numeric literal in C#?

To the power of zero

BC

1^0
1
2^0
1
0^0
1

JavaScript

1**0
1
2**0
1
0**0
1

C#

Console.WriteLine(Math.Pow(0, 0));

Output

1

C++

printf("%f", pow(0, 0));

Output

1.000000

SQL (Microsoft)

select power(0, 0);

Output

1

LibreOffice Calc

=0^0

Output

1

Microsoft Excel

=0^0

Output

Number Error

PSION Series 3a Calc app

More below on the PSION in another heading. Note if you run the second before last statement in OPL you’ll get a runtime error not a compile/translate time error.

  1**0
= 1
  2**0
= 1
  0**0 "Invalid arguments"
  2^7 "Illegal character"

Casio fx-991ES PLUS

Much to my annoyance it only has a exponent button and not a dedicated power of caret button. This basically means it semi expects some equation inside parentheses and opens a parenthesis. The redeeming factor is that it implicitly closes the parentheses.

2^(7
1^(0
2^(0
0^(0

Output

128
1
1
Math ERROR

It wont however implicitly open a parenthesis:

2+2)*2

Output

Syntax ERROR

This model has LCD display glare too.

Casio fx-83MS

This calculator I prefer over my fx-991ES PLUS. It has zero display glare and richer text due to the non glossy simpler LCD. It also has the caret button of which I prefer as it doesn’t try to open parentheses unless I choose to. So input is more readable when written out verbatim unlike the fx-991ES PLUS unless I manually close the parentheses. The buttons are also a bit better quality.

2^7
1^0
2^0
0^0

Output

128
1
1
Math ERROR

PSION Series 3a PDA

Calc app

Psion 3a Series User Manual

Using “Menu” button you get access to various functions just a few examples:

  • sin(x)
  • cos(x)
  • tan(x)
  • log(x)

In the menu is the option for power of, which generates a place holder: (x)**2

Sadly no pow(x) function.

  (2)**7
= 128

It works without parentheses also:

  2**7
= 128

The “Sheet” app works with the 2**7 but not 2^7 much like the “Calc” app.

The functions work with a space after comma.

  max(22,5)
= 22
  max(22, 5)
= 22

OPL programming

Another great old WYSIWYG website: Steve Litchfield - OPL programming

Random interesting page about Python on Symbian: Steve Litchfield - Introduction to Python on Nokia/Symbian Series 60

PSION Series 3a - PSION Programming Reference (Dave Stafford)

PROC myprog:
	PRINT "case insensitive"
	print "yes"
	get
	print "semi colon";
	print "foo"
	print "bar"
	get
ENDP

Output

case insensitive
yes

Key press

semi colonfoo
bar

Key press terminates. Interesting how the semi colon behaves.

Maths

print max (1, 6)
print 2**7
get

Output

6
128

A list of more REPLs

Only ones not mentioned on the first post.

comments powered by Disqus