Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168703
Image: ubuntu2004

This sheet will use Sage's comparison capabilities to investigate its way of handling different data types.

First, we check its handling of fractions:

x=1/7+1/7+1/7+1/7+1/7+1/7+1/7 print 'x = ',x x==1
x = 1 True

One interesting thing to note about Sage that distinguishes it from Matlab or Python is that since it can store fractions as rational numbers, it is able to correctly add them.

x=0 while x!=1: x=x+1/6 print 'x = ',x
x = 1

Note how Sage handles while loops. The general form is:

while condition:

          action

There is no need for an 'end' statement since Sage depends on indenting just like Python.

One other thing to note: '!=' is a common symbol for 'not equal to'. Other packages may not be able to do symbolic math, however. In limited precision arithmetic (like Matlab) x will never quite equal 1, causing this loop to run infinitely. It is best to use '<=' in this case. This will work in Sage and will avoid problems when translating into other languages.

x=0 while x<=1: x=x+1/6 print 'x = ',x