HP Prime and Casio fx-CG 50: Binary Conversions
x = 2 * (1 + n)^p
The program DECOM2 takes a real number, x, and decomposes it to the form of:
x = 2 * (1 + n)^p
HP Prime Program: DECOMP2
EXPORT DECOMP2(x)
BEGIN
// 2020-07-11 EWS
// x → 2^p*(1+n)
LOCAL s,p,n,str;
s:=ABS(x);
p:=FLOOR(LOG(s)/LOG(2));
n:=s/2^p-1;
str:=STRING(SIGN(X))+"*2^"+STRING(p)+
"*(1+"+STRING(n)+")";
RETURN str;
END;
Casio fx-CG 50 Program: DECOMP2
188 bytes
"2020-07-11 EWS"
"X"? → X
Abs X → S
Int (log S ÷ log 2) → P
S < 1 ⇒ P - 1 → P
S ÷ 2^P - 1 → N
X ÷ S → T
ClrText
Locate 1, 3, X
Locate 18, 3, "="
Locate 1, 5, T
Locate 4, 5, "×2^"
Locate 9, 5, P
Locate 18, 5, "×"
Locate 1, 6, "(1+"
Locate 5, 6, N
Locate 18, 6, ")"
Examples:
-720 = -1 * 2^9 * (1 + 0.40625)
0.7868 = 1 * 2^-1 * (1 + 0.5736)
Converting Numbers to Binary
The program RBIN converts any real number in base 10 to base 2, including non-integers and negative numbers. The result is displayed as a string. The HP Prime program has one string for output while the Casio fx-CG 50 uses two: Str1 for the integer part and Str2 for the fractional part.
RBIN will need two arguments: the number to be converted and the precision level.
For the precision level:
1 rounds the number to the nearest 1/2 before conversion.
2 rounds the number to the nearest 1/4 before conversion.
3 rounds the number to the nearest 1/8 before conversion.
4 rounds the number to the nearest 1/16 before conversion.
and so on.
HP Prime Program: RBIN
EXPORT RBIN(x,p)
BEGIN
// 2020-07-11 EWS
// Decimal to Binary
// decimal, precision
// for real x, rounded 1/2^p
LOCAL b,i,f,n,s,w,k,str;
s:=ABS(x);
b:=IP(s)+ROUND(FP(s)*2^p,0)/2^p;
str:="";
i:=IP(b);
f:=FP(b);
IF i≠0 THEN
n:=IP(LOG(i)/LOG(2));
FOR k FROM n DOWNTO 0 STEP 1 DO
w:=IP(i/2^k);
str:=str+STRING(w);
i:=i-w*2^k;
END;
END;
IF f≠0 THEN
str:=str+".";
FOR k FROM 0 TO p-1 DO
w:=IP(2*f);
str:=str+STRING(w);
f:=FP(2*f);
END;
END;
IF x<0 font="" then="">0>
str:="-"+str;
END;
RETURN str;
END;
Casio fx-CG 50: RBIN
372 bytes
"2020-07-11 EWS"
"DEC>BIN: NUM"? → X
"PRECISION"? → P
Abs X → S
Int S + RndFix(Frac S × 2^P, 0) ÷ 2^P → B
" " → Str 1
Int B → I
Frac B → F
If I ≠ 0
Then
Int (log I ÷ log 2) → N
For N → K To 0 Step -1
Int (I ÷ 2^K) → W
W = 0 ⇒ Str 1 + "0" → Str 1
W = 1 ⇒ Str 1 + "1" → Str 1
I - W × 2^K → I
Next
IfEnd
" " → Str 2
If F ≠ 0
Then
"." → Str 2
For 0 → K To P-1
Int (2F) → W
W = 0 ⇒ Str 2 + "0" → Str 2
W = 1 ⇒ Str 2 + "1" → Str 2
Frac (2F) → F
Next
IfEnd
X < 0 ⇒ "-" + Str 1 → Str 1
ClrText
Locate 1, 3, X
Locate 20, 3, "="
Locate 1, 5, Str 1
Locate 1, 6, Str 2
Examples:
RBIN(-0.985,3) returns "-1"
RBIN(-0.985,6) returns "-.111111"
RBIN(-0.985,9) returns "-.111111000"
RBIN(860.63,3) returns "1101011100.101"
RBIN(860.63,6) returns "1101011100.101000"
RBIN(860.63,9) returns "1101011100.101000011"
Take care,
Eddie
All original content copyright, © 2011-2020. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.
x = 2 * (1 + n)^p
The program DECOM2 takes a real number, x, and decomposes it to the form of:
x = 2 * (1 + n)^p
HP Prime Program: DECOMP2
EXPORT DECOMP2(x)
BEGIN
// 2020-07-11 EWS
// x → 2^p*(1+n)
LOCAL s,p,n,str;
s:=ABS(x);
p:=FLOOR(LOG(s)/LOG(2));
n:=s/2^p-1;
str:=STRING(SIGN(X))+"*2^"+STRING(p)+
"*(1+"+STRING(n)+")";
RETURN str;
END;
Casio fx-CG 50 Program: DECOMP2
188 bytes
"2020-07-11 EWS"
"X"? → X
Abs X → S
Int (log S ÷ log 2) → P
S < 1 ⇒ P - 1 → P
S ÷ 2^P - 1 → N
X ÷ S → T
ClrText
Locate 1, 3, X
Locate 18, 3, "="
Locate 1, 5, T
Locate 4, 5, "×2^"
Locate 9, 5, P
Locate 18, 5, "×"
Locate 1, 6, "(1+"
Locate 5, 6, N
Locate 18, 6, ")"
Examples:
-720 = -1 * 2^9 * (1 + 0.40625)
0.7868 = 1 * 2^-1 * (1 + 0.5736)
Converting Numbers to Binary
The program RBIN converts any real number in base 10 to base 2, including non-integers and negative numbers. The result is displayed as a string. The HP Prime program has one string for output while the Casio fx-CG 50 uses two: Str1 for the integer part and Str2 for the fractional part.
RBIN will need two arguments: the number to be converted and the precision level.
For the precision level:
1 rounds the number to the nearest 1/2 before conversion.
2 rounds the number to the nearest 1/4 before conversion.
3 rounds the number to the nearest 1/8 before conversion.
4 rounds the number to the nearest 1/16 before conversion.
and so on.
HP Prime Program: RBIN
EXPORT RBIN(x,p)
BEGIN
// 2020-07-11 EWS
// Decimal to Binary
// decimal, precision
// for real x, rounded 1/2^p
LOCAL b,i,f,n,s,w,k,str;
s:=ABS(x);
b:=IP(s)+ROUND(FP(s)*2^p,0)/2^p;
str:="";
i:=IP(b);
f:=FP(b);
IF i≠0 THEN
n:=IP(LOG(i)/LOG(2));
FOR k FROM n DOWNTO 0 STEP 1 DO
w:=IP(i/2^k);
str:=str+STRING(w);
i:=i-w*2^k;
END;
END;
IF f≠0 THEN
str:=str+".";
FOR k FROM 0 TO p-1 DO
w:=IP(2*f);
str:=str+STRING(w);
f:=FP(2*f);
END;
END;
IF x<0 font="" then="">0>
str:="-"+str;
END;
RETURN str;
END;
Casio fx-CG 50: RBIN
372 bytes
"2020-07-11 EWS"
"DEC>BIN: NUM"? → X
"PRECISION"? → P
Abs X → S
Int S + RndFix(Frac S × 2^P, 0) ÷ 2^P → B
" " → Str 1
Int B → I
Frac B → F
If I ≠ 0
Then
Int (log I ÷ log 2) → N
For N → K To 0 Step -1
Int (I ÷ 2^K) → W
W = 0 ⇒ Str 1 + "0" → Str 1
W = 1 ⇒ Str 1 + "1" → Str 1
I - W × 2^K → I
Next
IfEnd
" " → Str 2
If F ≠ 0
Then
"." → Str 2
For 0 → K To P-1
Int (2F) → W
W = 0 ⇒ Str 2 + "0" → Str 2
W = 1 ⇒ Str 2 + "1" → Str 2
Frac (2F) → F
Next
IfEnd
X < 0 ⇒ "-" + Str 1 → Str 1
ClrText
Locate 1, 3, X
Locate 20, 3, "="
Locate 1, 5, Str 1
Locate 1, 6, Str 2
Examples:
RBIN(-0.985,3) returns "-1"
RBIN(-0.985,6) returns "-.111111"
RBIN(-0.985,9) returns "-.111111000"
RBIN(860.63,3) returns "1101011100.101"
RBIN(860.63,6) returns "1101011100.101000"
RBIN(860.63,9) returns "1101011100.101000011"
Take care,
Eddie
All original content copyright, © 2011-2020. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.