Convert from fraction to decimal and vice versa, TI 83 basic - ti-basic

I have a code to calculate the slope of a line if two points are known; however, it only returns the slope in decimal format. I'd like the ability to choose to convert the decimal to a fraction. Here's what I've done so far:
:Disp " "
:Input "PAIR 1: ",T
:Input "PAIR 2: ",U
:((LU(2)-LT(2))/(LU(1)-LT(2)))->M
:Disp "Slope Is: ",M
:Input "F>>D? Y/N: ",Str0
:If Str0="Y"
:(M>F<>D)->O
:Disp O
When I run this, I get ERROR: SYNTAX on the conversion step (the actual slope calculation completes successfully.)
**Some of the code I've typed above is not exactly how it appears in the TI 84, but it's the best I can do with a QWERTY keyboard.

►Frac and ►F◄►D are display tokens, not conversion tokens
TI calculators can only store numbers one way: in their proprietary floating-point format. This means that numbers cannot be "converted" from fraction to decimal or vice versa, only displayed as fractions or decimals. Thus, storing to anything (e.g. 3►F◄►D→X) will throw a syntax error.
Display modes are changed using the Auto/Dec/Frac tokens in the MODE menu; ►Frac overrides the mode and displays as a fraction approximation, ►Dec displays as decimal, and ►F◄►D tells the calculator to display in the opposite display mode from its current setting.
Quoting from this page:
►Frac attempts to display the input in fraction form. It only works on the home screen outside a program, or with the Disp and Pause commands in a program.

The trouble is in the line
(M>F<>D)->O
Disp O
There's no reason to format it this way when it can simply be expressed as
Disp M>F<>D
which calculates properly.

One way to convert from decimal to a fraction is to use the Euclidean Algorithm. Here it is in ti-basic
:Ans→X:{1,abs(Ans
:Repeat E‾9>Ans(2
:abs(Ans(2){1,fPart(Ans(1)/Ans(2
:End
:round({X,1}/Ans(1),0
:Ans/gcd(Ans(1),Ans(2
Ans is the decimal you wish to convert
and E‾9 is the same as 10^(-9)

Related

How to make the π and 16 be on the same line?

So, I'm making a program on my TI-84 Plus that finds the area of a circle. I want it to display the approximate answer and the exact answer, but I can't get the latter to work. The code is this (I realize that it isn't very efficient):
Prompt R
ClrHome
(πR²)→A
(R²)→B
round(B,2)→B
Disp "A="
Disp ""
Disp "APPROXIMATION:"
Disp A
Disp "EXACT:"
Disp "π",B
Disp "PRESS ENTER"
Pause
However, it displays like this (when the radius is 4):
A=
APPROXIMATION:
50.27
EXACT:
π
16
PRESS ENTER
I would like to know how to make the π and 16 (in this case) be on the same line, with π before 16, and have it work for all number of digits. I've tried to use the Output( command, but if B is over 2 digits, it goes to the next line. Thanks!
Perhaps use Output(X,Y,"π") and Output(X,Y-3,B)
This is how I would do this:
:Prompt R
:ClrHome
:(πR²)→A
:(R²)→B
:log(B)→C
:round(B,2)→B
:Output(1,1,"A")
:Output(3,1,"APPROXIMATION:")
:Output(3,16,A)
:Output(5,1,"EXACT:")
:Output(5,8,B)
:Output(5,11+C,"π")
:Output(7,1,"PRESS ENTER")
:Pause
Basically, once the calculations are made, in this prgm text, the program would use log(B)→C to find how many spaces to move the π to the right. After that, it would output A at the first line of text, the approximation at the third line of the Home Screen grid (for Output), and the exact approximation, in terms of π, at the fifth line of the Home Screen grid (for Output).
Hope this helps!
Personally, I tend to use Output( for displaying text, but there are another two ways you could do this.
You could use Text( (under the Draw menu) to write text to the graph screen. For this, you need to make sure that all graphs are off, axes are off, and grid is off (grid only applies for TI84 +C Silver Edition as far as I know).
You can use Disp to display a string containing the values you want. For this, you need to turn the value of B into a string. There are a few ways you can do this, one posted here.

Fortran format 1P10E11.3

Does anyone know what this format line means in fortran:
FORMAT(1x,F7.0,2x,1P10E11.3)
I know the first part is one repetition of float number but I don't understand how many exponential data points are read in the second part and what that P is for.
The P format shifts the decimal point. The behavior is different on input and output. On output, applied to an E format, it shifts the decimal point of the value before the exponent and changes the values of the exponent such that the value of the number is unchanged. If plain E would output 0.123E+3, 1PE will output 1.230E+2. On input it changes the value read -- use with great caution or not at all. Another "gotcha" is that P stays in effect for the rest of the format, until another P specifier appears in the format, e.g., 0P to reset. One of the newer G, ES or EN formats are generally better than the combination of P and E.

How do you "fix" decimal points when converting to DMS?

In TI-Basic, there's a Fix function to limit the number of displayed decimal places. For example, Fix 2 would display only 2 decimal digits. However, when I try to convert a number to Degree-Minute-Second notation, I sometimes get more than the number of "fixed" decimal digits. For example,
1.12345678901
Float
Disp Ans►DMS
Fix 2
Disp Ans►DMS
Float
Disp Ans
Fix 2
Disp Ans
displays
1°7'24.444"
1°7'24.444"
1.123456789
1.12
The normal decimals act as expected. However, I would expect the second line to display 1°7'24.44. Is this possible? Or would I have to somehow convert it to a string and prune afterwards? (Keep in mind that I want to shorten the decimal because of the display constraints; I want to display text next to it without overlap).
extra info: TI-84+ Silver Ed'n, OS version 2.55 w/MathPrint
►DMS will display 0 to 3 digits after the decimal point, solely depending on the length of the decimal. The Fix command, set programmatically or through MODE does not affect this.
Storing a number formatted in DMS in a variable will undo the DMS formatting, and it cannot be stored in a string.
My suggestion would be isolating the degrees, minutes, and seconds in separate variables and working with them from there. In this way, they would also all be affected by the Fix command.

Why does Round[2.75,0.1] return 2.800000000003?

Mathematica 8.0.1
Any one could explain what would be the logic behind this result
In[24]:= Round[10.75, .1]
Out[24]= 10.8
In[29]:= Round[2.75, .1]
Out[29]= 2.8000000000000003
I have expected the second result above to be 2.8?
EDIT 1:
I was trying to do the above for formatting purposes only to make the number fit in the space. I ended up doing the following to get the result I want:
In[41]:= NumberForm[2.75,2]
Out[41] 2.8
I wish Mathematica has printf() like formatting function. I find formatting numbers in Mathematica for exact field width and form a little awkward compared to using printf() formatting rules.
EDIT 2:
I tried $MaxExtraPrecision=1000 on some number I was trying for format/round, but it did not work, that is why I posted this question. Here it is
In[42]:= $MaxExtraPrecision=1000;
Round[2035.7520395261859,.1]
Out[43]= 2035.8000000000002
In[46]:= $MaxExtraPrecision=50;
Round[2.75,.1]
Out[47]= 2.8000000000000003
EDIT 3:
I found this way, to format a number to one decimal point only. Use Numberform, but first need to find what n-digit precision to use by counting the number of digits to the left of the decimal point, then adding 1.
In[56]:= x=2035.7520395261859;
NumberForm[x,IntegerLength[Round#x]+1]
Out[57]//NumberForm= 2035.8
EDIT 4:
The above (Edit 3) did not work for numbers such as
a=2.67301785 10^7
After some trials, I found Accounting Form to do what I want. AccountingForm gets rid of the 10^n form which NumberForm did not:
In[76]:= x=2035.7520395261859;
AccountingForm[x,IntegerLength[Round#x]+1]
Out[77]//AccountingForm= 2035.8
In[78]:= x=2.67301785 10^7;
AccountingForm[x,IntegerLength[Round#x]+1]
Out[79]//AccountingForm= 26730178.5
For formatting numerical values, the best language I found was Fortran, followed COBOL and also by those languages that use or support printf() standard formatting. With Mathematica, one can do such formatting I am sure, but it sure seems too complicated to me. I never understood why Mathematics does not have Printf[].
Not all decimal (base 10) numbers with a finite number of digits are representable in binary (base 2) with a finite number of digits. E.g. 0.1 is not representable in binary, just like 1/3 ~= 0.33333... is not representable in decimal. Mathematica (and other software) will only use a limited number of decimal digits when showing the number to hide this effect. However, occasionally it might happen that enough decimal digits are shown that the mismatch becomes visible.
http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding
EDIT
This command will show you what happens when you find the closes binary representation of 0.1 using 20 binary digits, then convert it back to decimal:
RealDigits[FromDigits[RealDigits[1/10, 2, 20], 2], 10]
The number is stored in base 2, rather than base 10 (decimal). It's impossible to represent 2.8 in base 2, so it uses the closest value: 2.8000000000000003
Number/AccountingForm can take a list in the second argument, the second item of which is how many digits after the decimal place to show:
In[61]:= x=2035.7520395261859;
In[62]:= AccountingForm[x,{Infinity,3}]
Out[62]//AccountingForm= 2035.752
Perhaps this is useful.

How to do high precision float point arithmetics in mathematica

In Mma, for example, I want to calculate
1.0492843824838929890231*0.2323432432432432^3
But it does not show the full precision. I tried N or various other functions but none seemed to work. How to achieve this? Many thanks.
When you specify numbers using decimal point, it takes them to have MachinePrecision, roughly 16 digits, hence the results typically have less than 16 meaningful digits. You can do infinite precision by using rational/algebraic numbers. If you want finite precision that's better than default, specify your numbers like this
123.23`100
This makes Mathematica interpret the number as having 100 digits of precision. So you can do
ans=1.0492843824838929890231`100*0.2323432432432432`100^3
Check precision of the final answer using Precision
Precision[ans]
Check tutorial/ArbitraryPrecisionNumbers for more details
You may do:
r[x_]:=Rationalize[x,0];
n = r#1.0492843824838929890231 (r#0.2323432432432432)^3
Out:
228598965838025665886943284771018147212124/17369643723462006556253010609136949809542531
And now, for example
N[n,100]
0.01316083216659453615093767083090600540780118249299143245357391544869\
928014026433963352910151464006549
Sometimes you just want to see more of the machine precision result. These are a few methods.
(1) Put the cursor at the end of the output line, and press Enter (not on the numeric keypad) to copy the output to a new input line, showing all digits.
(2) Use InputForm as in InputForm[1.0/7]
(3) Change the setting of PrintPrecision using the Options Inspector.

Resources