# Sage Essentials: formatting numbers

I've added the following information to the Sage Essentials page on formatting the numbers in Sage output:

Formatting numbers

Chances are you'll find Sage gives you a number in a format that doesn't look very nice. You can change the formatting by converting the number to a string and then using the formatting options for string to clean up the representation. Run the following code:

print("The square root of 2: "+str(sqrt(2)))#Print sqrt(2) symbolically
a=float(sqrt(2))#force sqrt(2) to a floating point number
print("The square root of 2 is "+str(a)+"but now you might not like the format of the number.")#Print the number
print("To control the format of the number, try: The square root of 2 is {0:.3f}".format(a))#print 3 digits after decimal
b=float(sqrt(3))
c=float(a+b)
print("Multiple arguments can also be handled: the sqrt(2)+sqrt(3) is just {0:.5f} + {1:.5f} = {2:.5f}".format(a,b,c))

and you'll get this output:

$latex \sqrt{2}$ is represented symbolically as sqrt(2) until it is converted to a floating a point number with:

a=float(sqrt(2))#force sqrt(2) to a floating point number

at which point now you have Sage deciding for you how the decimal is represented. You can tell Sage how you want your numbers displayed using the format method of Python as explained in detail here. The key line in our code above is

print("Multiple arguments can also be handled: the sqrt(2)+sqrt(3) is just {0:.5f} + {1:.5f} = {2:.5f}".format(a,b,c))print("Multiple arguments can also be handled: the sqrt(2)+sqrt(3) is just {0:.5f} + {1:.5f} = {2:.5f}".format(a,b,c))

There are 3 numbers used in the print statement, referred to as 0, 1, and 2. When it's time to print a number we give instruction as to how the number should be formatted. The argument {0:.5f} instructs that the 0th number should show 5 digits after the decimal. After instructing what the format will be for each number you finish it off with the format(a,b,c) to define a as the 0th number, b as the first number, and c as the 2nd number. As the Python documentation tells us, decimals can be represented as fixed point (f), exponential (e), general format (g), n as number and % as percents.

Older methods of string format still work but are being deprecated so use the format method.

# Handout: Trig Formulas and Identities

I've put together a handout of the various trigonometric formulas and identities; it's posted on the Handouts page. The tex code is included so if you know $latex \LaTeX$ you can customize it to your liking. In order to make that easier, I've got code in the preamble:

\newcommand{\AngA}{\ensuremath{\alpha}}%Define the symbols used for angles
\newcommand{\AngB}{\ensuremath{\beta}}
\newcommand{\AngC}{\ensuremath{\theta}}

By defining the angles through \newcommand you can change  \AngA from $latex \theta$ to anything you'd like. The same goes for \AngB and \AngC. Changing the angle in one place changes all the angles.

# Sage tutoring: Midpoint formula and Distance formula

Learning math involves a lot of repetition. As a teacher you give your students all the time you can but they still have to go home and work the problems over and over. The problem is they get stuck and unless they have a parent or friend who can help them many are likely to give up. A computer can help: it's available whenever the student is ready to practice, and it can create problem after problem until the student they feel they've achieved mastery.

This year I'm going to try putting together Sage code that can help students practice some essential problems they need to know. I've started with the Midpoint Formula and the Distance formula. As the picture shows the Sage Interact manipulative can show the step by step solution. There's a checkbox to turn off the solution. The program can potentially be used in the classroom to generate the problems that you cover during the lesson.

You can find the code on the Sage In the Classroom page. Maybe it can help you and your students as well.

# tkz-euclide: circles

The tkz-euclide package is an essential package for designing geometrical drawings because it has a variety of macros to handle the more intricate details, such as a line tangent to a circle. The manual is in French, however, which means if you're like me and you don't use the package frequently then you might have a difficult time re-learning the material.

I've added a page summarizing circle macros in the tkz-euclide package. It contains code to illustrate the basic ideas which can be a useful starting point when you want to create your own diagram. The new page is on the sidebar or you can click here.

# Sage in the Classroom page

I'm going to make an effort to give Sage a more prominent place  in the classroom. I've started a new page, Sage in the Classroom, where I'll be posting the code for Sage Interact manipulatives that can be worked into the classroom setting more easily. First up is a simple manipulative for showing some basic shapes that students should know at the beginning of a precalculus course. The term parent function is often used for some of the shapes (the vertical line isn't a function).

The basic idea is that this manipulative can save me (and other teachers) some time: there's no need to take the time to draw each graph on the board. Just load the manipulative, flip through each of the basic shapes and dive in. Which function is this? What's the domain? What's the range? Where is the function decreasing? Where is it increasing?

The new page is listed on the sidebar, or you can click here to get there faster.

# The Importance of Fractions

Fractions don't get much respect in the digital age with calculators being so pervasive in our culture. Most of my students are seriously deficient in working with fractions and will always go to decimals. Of course, they'll still make mistakes with decimal calculations; they haven't mastered those either. But that's more understandable since working with decimals is more difficult (even if my class thinks otherwise). I made more of an effort this year to gently push students towards fractions by emphasizing that there are times when calculating with fractions is much easier than decimals, so it is important that you can calculate in both as the problem demands.

Work a problem like this with your class: Find the volume of a sphere with radius 1.25 meters. The answer is, of course, $latex \frac{4}{3}(1.25)^3\pi$ but have fun getting the (approximate) decimal answer without using calculator: $latex 2.60416666667\pi$. Students typically make mistakes on the placement of the decimal when cubing $latex (1.25)^3$; multiply that by 4 and divide by 3 and you'll have a lot of trouble finding someone who can actually get the correct answer.

But the point I want to make is that a simple math problem becomes too tedious if you want to express the answer using decimals whereas it's quick and easy using fractions. Calculate the answer to the same problem using fractions: $latex \frac{4}{3}\pi\left(\frac{5}{4}\right)^3$ which gives $latex \frac{4}{3}\frac{125}{(4)(4)(4)}\pi=\frac{125}{(3)(4)(4)}\pi=\frac{125}{48}\pi$. To express the answer using fractions you only need to multiply and divide integers. Fractions get you the exact answer, quickly, without ever needing a calculator.

My effort to push students to use fractions wasn't successful; their background is just too poor. How is it these students keep getting passed up to the high school level? That's a rhetorical question, but I really would like to observe what goes on in a middle school math class: I inherit students who are already seriously deficient in the basic skills necessary for success in math.

I'm going make more of an effort to require answers in fractional form. Mastery of fractions is still an essential skill for every math student, even in the digital age.

# Sage Essentials: reading from a file

Inspired by a post at AskSage, I've added a section to the Sage Essentials page on using Sage to read from a file on your computer and process the data. This won't work with a Sage Cell Server, you'll need Sage installed on your computer.

To make it more relevant to all you teachers out there, the program will take student test data from a .csv file, perform some calculations, and print out the results. The output is shown above. You can read more at the bottom of this page.