Jump to content

Convert decimal number to rational: Difference between revisions

no edit summary
m (syntax highlighting fixup automation)
No edit summary
Line 2,894:
dbl2frac 0.518518 -> 14 27, expected 42 81
~ $
 
=={{header|TI SR-56}}==
{| class="wikitable"
|+ Texas Instruments SR-56 Program Listing for "Convert decimal number to rational"
|-
! Display !! Key !! Display !! Key !! Display !! Key !! Display !! Key
|-
| 00 33 || STO || 25 00 || 0 || 50 || || 75 ||
|-
| 01 00 || 0 || 26 64 || * || 51 || || 76 ||
|-
| 02 00 || 0 || 27 34 || RCL || 52 || || 77 ||
|-
| 03 33 || STO || 28 01 || 1 || 53 || || 78 ||
|-
| 04 01 || 1 || 29 94 || = || 54 || || 79 ||
|-
| 05 56 || *CP || 30 41 || R/S || 55 || || 80 ||
|-
| 06 01 || 1 || 31 || || 56 || || 81 ||
|-
| 07 35 || SUM || 32 || || 57 || || 82 ||
|-
| 08 01 || 1 || 33 || || 58 || || 83 ||
|-
| 09 34 || RCL || 34 || || 59 || || 84 ||
|-
| 10 01 || 1 || 35 || || 60 || || 85 ||
|-
| 11 64 || * || 36 || || 61 || || 86 ||
|-
| 12 34 || RCL || 37 || || 62 || || 87 ||
|-
| 13 00 || 0 || 38 || || 63 || || 88 ||
|-
| 14 94 || = || 39 || || 64 || || 89 ||
|-
| 15 12 || Inv || 40 || || 65 || || 90 ||
|-
| 16 29 || *Int || 41 || || 66 || || 91 ||
|-
| 17 12 || Inv || 42 || || 67 || || 92 ||
|-
| 18 37 || *x=t || 43 || || 68 || || 93 ||
|-
| 19 00 || 0 || 44 || || 69 || || 94 ||
|-
| 20 06 || 6 || 45 || || 70 || || 95 ||
|-
| 21 34 || RCL || 46 || || 71 || || 96 ||
|-
| 22 01 || 1 || 47 || || 72 || || 97 ||
|-
| 23 32 || x<>t || 48 || || 73 || || 98 ||
|-
| 24 34 || RCL || 49 || || 74 || || 99 ||
|}
 
Asterisk denotes 2nd function key.
 
{| class="wikitable"
|+ Register allocation
|-
| 0: Rational || 1: Denominator|| 2: Unused || 3: Unused || 4: Unused
|-
| 5: Unused || 6: Unused || 7: Unused || 8: Unused || 9: Unused
|}
 
Annotated listing:
<syntaxhighlight lang="text">
STO 0 // Rational := User Input
0 STO 1 // Denominator := 0
*CP // RegT := 0
1 SUM 1 // Denominator += 1
RCL 1 * RCL 0 = Inv *Int // Find fractional part of Rational * Denominator
Inv *x=t 0 6 // If it is nonzero loop back to instruction 6
RCL 1 x<>t // Report denominator
RCL 0 * RCL 1 = // Report numerator
R/S // End
</syntaxhighlight>
 
'''Usage:'''
 
Enter the rational and then press RST R/S. Once a suitable fraction is found, the numerator will be displayed. Press x<>t to toggle between the numerator and denominator.
 
{{in}}
 
0.875 RST R/S
 
{{out}}
 
After about five seconds, '''3''' will appear on the screen. Press x<>t and '''8''' will appear on the screen. The fraction is 3/8.
 
=={{header|Vala}}==
27

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.