Continued fraction/Arithmetic/Construct from rational number: Difference between revisions
Content added Content deleted
m (→{{header|ALGOL 68}}: Typo) |
(→{{header|ALGOL 68}}: Let's use the Algol 68 MOD operator after all) |
||
Line 71: | Line 71: | ||
{{Trans|C}} |
{{Trans|C}} |
||
...with code from the Arithmetic/Rational task.<br> |
...with code from the Arithmetic/Rational task.<br> |
||
The continued fraction expansion of -151/77 is sensitive to whether the language modulo operator follows the mathematical definition or the C definition.<br> |
|||
Note that Algol 68's MOD operator uses the mathematical definition (rounds towards -infinity) which differs from C and many other languages. The remainder is calculated here as it would be in C, so the results for -157//77 agree with the other samples. |
|||
Algol 68's MOD operator uses the mathematical definition (rounds towards -infinity), so the results for -157//77 agree with the EDSAC, J and a few other sdamples. Most other samples calculate the remainder using the C definotion. |
|||
<lang algol68>BEGIN # construct continued fraction representations of rational numbers # |
<lang algol68>BEGIN # construct continued fraction representations of rational numbers # |
||
# Translated from the C sample # |
# Translated from the C sample # |
||
Line 131: | Line 132: | ||
INT prev numerator = numerator; |
INT prev numerator = numerator; |
||
numerator := denominator; |
numerator := denominator; |
||
denominator := prev numerator |
denominator := prev numerator MOD denominator; |
||
quotient |
quotient |
||
FI # r2cf # ; |
FI # r2cf # ; |
||
Line 163: | Line 164: | ||
For N = 13, D = 11 : 1 5 2 |
For N = 13, D = 11 : 1 5 2 |
||
For N = 22, D = 7 : 3 7 |
For N = 22, D = 7 : 3 7 |
||
For N = -151, D = 77 : -1 |
For N = -151, D = 77 : -1 25 1 2 |
||
Running for root2 : |
Running for root2 : |