Arithmetic/Rational: Difference between revisions
Content added Content deleted
(Added BBC BASIC) |
(Added Maple implementation) |
||
Line 1,594: | Line 1,594: | ||
end |
end |
||
print(findperfs(2^19))</lang> |
print(findperfs(2^19))</lang> |
||
=={{header|Maple}}== |
|||
Maple has full built-in support for arithmetic with fractions (rational numbers). Fractions are treated like any other number in Maple. |
|||
<lang Maple> |
|||
> a := 3 / 5; |
|||
a := 3/5 |
|||
> numer( a ); |
|||
3 |
|||
> denom( a ); |
|||
5 |
|||
</lang> |
|||
However, while you can enter a fraction such as "4/6", it will automatically be reduced so that the numerator and denominator have no common factor: |
|||
<lang Maple> |
|||
> b := 4 / 6; |
|||
b := 2/3 |
|||
</lang> |
|||
All the standard arithmetic operators work with rational numbers. It is not necessary to call any special routines. |
|||
<lang Maple> |
|||
> a + b; |
|||
19 |
|||
-- |
|||
15 |
|||
> a * b; |
|||
2/5 |
|||
> a / b; |
|||
9/10 |
|||
> a - b; |
|||
-1 |
|||
-- |
|||
15 |
|||
> a + 1; |
|||
8/5 |
|||
> a - 1; |
|||
-2/5 |
|||
</lang> |
|||
Notice that fractions are treated as exact quantities; they are not converted to floats. However, you can get a floating point approximation to any desired accuracy by applying the function evalf to a fraction. |
|||
<lang Maple> |
|||
> evalf( 22 / 7 ); # default is 10 digits |
|||
3.142857143 |
|||
> evalf[100]( 22 / 7 ); # 100 digits |
|||
3.142857142857142857142857142857142857142857142857142857142857142857\ |
|||
142857142857142857142857142857143 |
|||
</lang> |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |