September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,3 @@
|
|||
' Formatted numeric output
|
||||
n = 7.125
|
||||
PRINT n FORMAT "%09.3f\n"
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Print number n, using at least c characters.
|
||||
*
|
||||
* Different from normal, this function:
|
||||
* 1. Uses the current ibase (not the obase) to print the number.
|
||||
* 2. Prunes "0" digits from the right, so p(1.500, 1) prints "1.5".
|
||||
* 3. Pads "0" digits to the left, so p(-1.5, 6) prints "-001.5".
|
||||
* 4. Never prints a newline.
|
||||
*
|
||||
* Use an assignment, as t = p(1.5, 1), to discard the return value
|
||||
* from this function so that bc not prints the return value.
|
||||
*/
|
||||
define p(n, c) {
|
||||
auto d, d[], f, f[], i, m, r, s, v
|
||||
s = scale /* Save original scale. */
|
||||
|
||||
if (n < 0) {
|
||||
"-" /* Print negative sign. */
|
||||
c -= 1
|
||||
n = -n /* Remove negative sign from n. */
|
||||
}
|
||||
|
||||
/* d[] takes digits before the radix point. */
|
||||
scale = 0
|
||||
for (m = n / 1; m != 0; m /= 10) d[d++] = m % 10
|
||||
|
||||
/* f[] takes digits after the radix point. */
|
||||
r = n - (n / 1) /* r is these digits. */
|
||||
scale = scale(n)
|
||||
f = -1 /* f counts the digits of r. */
|
||||
for (m = r + 1; m != 0; m /= 10) f += 1
|
||||
scale = 0
|
||||
r = r * (10 ^ f) / 1 /* Remove radix point from r. */
|
||||
if (r != 0) {
|
||||
while (r % 10 == 0) { /* Prune digits. */
|
||||
f -= 1
|
||||
r /= 10
|
||||
}
|
||||
for (i = 0; i < f; i++) {
|
||||
f[i] = r % 10
|
||||
r /= 10
|
||||
}
|
||||
}
|
||||
|
||||
/* Pad "0" digits to reach c characters. */
|
||||
c -= d
|
||||
if (f > 0) c -= 1 + f
|
||||
for (1; c > 0; c--) "0" /* Print "0". */
|
||||
|
||||
/* i = index, m = maximum index, r = digit to print. */
|
||||
m = d + f
|
||||
for (i = 1; i <= m; i++) {
|
||||
if (i <= d) r = d[d - i]
|
||||
if (i > d) r = f[m - i]
|
||||
if (i == d + 1) "." /* Print radix point. */
|
||||
|
||||
v = 0
|
||||
if (r == v++) "0" /* Print digit. */
|
||||
if (r == v++) "1"
|
||||
if (r == v++) "2" /* r == 2 might not work, */
|
||||
if (r == v++) "3" /* unless ibase is ten. */
|
||||
if (r == v++) "4"
|
||||
if (r == v++) "5"
|
||||
if (r == v++) "6"
|
||||
if (r == v++) "7"
|
||||
if (r == v++) "8"
|
||||
if (r == v++) "9"
|
||||
if (r == v++) "A"
|
||||
if (r == v++) "B"
|
||||
if (r == v++) "C"
|
||||
if (r == v++) "D"
|
||||
if (r == v++) "E"
|
||||
if (r == v++) "F"
|
||||
}
|
||||
|
||||
scale = s /* Restore original scale. */
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
x = 7.125
|
||||
"Decimal: "; t = p(x, 9); "
|
||||
"
|
||||
ibase = 16
|
||||
"Hexadecimal: "; t = p(x, 9); "
|
||||
"
|
||||
ibase = 2
|
||||
"Binary: "; t = p(x, 1001); "
|
||||
"
|
||||
quit
|
||||
112
Task/Formatted-numeric-output/Dc/formatted-numeric-output-1.dc
Normal file
112
Task/Formatted-numeric-output/Dc/formatted-numeric-output-1.dc
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
[*
|
||||
* (n) (c) lpx
|
||||
* Print number n, using at least c characters.
|
||||
*
|
||||
* Different from normal, this function:
|
||||
* 1. Uses the current ibase (not the obase) to print the number.
|
||||
* 2. Prunes "0" digits from the right, so [1.500 1 lxp] prints "1.5".
|
||||
* 3. Pads "0" digits to the left, so [_1.5 6 lxp] prints "-001.5".
|
||||
* 4. Never prints a newline.
|
||||
*]sz
|
||||
[
|
||||
Sc Sn [Local n, c = from stack.]sz
|
||||
K Ss [Local s = original scale.]sz
|
||||
[Reserve local variables D, F, I, L.]sz
|
||||
0 SD 0 SF 0 SI 0 SL
|
||||
|
||||
[ [If n < 0:]sz
|
||||
[-]P [Print negative sign.]sz
|
||||
lc 1 - sc [Decrement c.]sz
|
||||
0 ln - sn [Negate n.]sz
|
||||
]sI 0 ln <I
|
||||
|
||||
[*
|
||||
* Array D[] takes digits before the radix point.
|
||||
*]sz
|
||||
0 k [scale = 0]sz
|
||||
0 Sd [Local d = 0]sz
|
||||
ln 1 / [Push digits before radix point.]sz
|
||||
[ [Loop to fill D[]:]sz
|
||||
d 10 % ld :D [D[d] = next digit.]sz
|
||||
ld 1 + sd [Increment d.]sz
|
||||
10 / [Remove digit.]sz
|
||||
d 0 !=L [Loop until no digits.]sz
|
||||
]sL d 0 !=L
|
||||
sz [Pop digits.]sz
|
||||
|
||||
[*
|
||||
* Array F[] takes digits after the radix point.
|
||||
*]sz
|
||||
ln ln 1 / - [Push digits after radix point.]sz
|
||||
d X k [scale = enough.]sz
|
||||
_1 Sf [Local f = -1]sz
|
||||
d 1 + [Push 1 + digits after radix point.]sz
|
||||
[ [Loop to count digits:]sz
|
||||
lf 1 + sf [Increment f.]sz
|
||||
10 / [Remove digit.]sz
|
||||
d 0 !=L [Loop until no digits.]sz
|
||||
]sL d 0 !=L
|
||||
sz [Pop 1 + digits.]sz
|
||||
0 k [scale = 0]sz
|
||||
10 lf ^ * 1 / [Remove radix point from digits.]sz
|
||||
[ [Loop to prune digits:]sz
|
||||
lf 1 - sf [Decrement f.]sz
|
||||
10 / [Remove digit.]sz
|
||||
d 10 % 0 =L [Loop while last digit is 0.]sz
|
||||
]sL d 10 % 0 =L
|
||||
0 Si [Local i = 0]sz
|
||||
[ [Loop to fill F[]:]sz
|
||||
d 10 % li :F [F[i] = next digit.]sz
|
||||
10 / [Remove digit.]sz
|
||||
li 1 + si [Increment i.]sz
|
||||
lf li <L [Loop while i < f.]sz
|
||||
]sL lf li <L
|
||||
sz [Pop digits.]sz
|
||||
|
||||
lc ld - [Push count = c - d.]sz
|
||||
[ [If f > 0:]sz
|
||||
1 lf + - [Subtract 1 radix point + f from count.]sz
|
||||
]sI 0 lf >I
|
||||
[ [Loop:]sz
|
||||
[0]P [Print a padding "0".]sz
|
||||
1 - [Decrement count.]sz
|
||||
d 0 <L [Loop while count > 0.]sz
|
||||
]sL d 0 <L
|
||||
sz [Pop count.]sz
|
||||
|
||||
[ [Local function (digit) lPx:]sz
|
||||
[ [Execute:]sz
|
||||
[*
|
||||
* Push the string that matches the digit.
|
||||
*]sz
|
||||
[[0] 2Q]sI d 0 =I [[1] 2Q]sI d 1 =I [[2] 2Q]sI d 2 =I [[3] 2Q]sI d 3 =I
|
||||
[[4] 2Q]sI d 4 =I [[5] 2Q]sI d 5 =I [[6] 2Q]sI d 6 =I [[7] 2Q]sI d 7 =I
|
||||
[[8] 2Q]sI d 8 =I [[9] 2Q]sI d 9 =I [[A] 2Q]sI d A =I [[B] 2Q]sI d B =I
|
||||
[[C] 2Q]sI d C =I [[D] 2Q]sI d D =I [[E] 2Q]sI d E =I [[F] 2Q]sI d F =I
|
||||
[?] [Else push "?".]sz
|
||||
]x
|
||||
P [Print the string.]sz
|
||||
sz [Pop the digit.]sz
|
||||
]SP
|
||||
ld [Push counter = d.]sz
|
||||
[ [Loop:]sz
|
||||
1 - [Decrement counter.]sz
|
||||
d ;D lPx [Print digit D[counter].]sz
|
||||
d 0 <L [Loop while counter > 0.]sz
|
||||
]sL d 0 <L
|
||||
sz [Pop counter.]sz
|
||||
[ [If f > 0:]sz
|
||||
[.]P [Print radix point.]sz
|
||||
lf [Push counter = f.]sz
|
||||
[ [Loop:]sz
|
||||
1 - [Decrement counter.]sz
|
||||
d ;F lPx [Print digit F[counter].]sz
|
||||
d 0 <L [Loop while counter > 0.]sz
|
||||
]sL d 0 <L
|
||||
sz [Pop counter.]sz
|
||||
]sI 0 lf >I
|
||||
|
||||
[Restore variables n, c, d, f, D, F, L, I, P.]sz
|
||||
Lnsz Lcsz Ldsz Lfsz LDsz LFsz LLsz LIsz LPsz
|
||||
Ls k [Restore variable s. Restore original scale.]sz
|
||||
]sp
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
7.125 sx
|
||||
[Decimal: ]P lx 9 lpx [
|
||||
]P 16 i [Hexadecimal: ]P lx 9 lpx [
|
||||
]P 2 i [Binary: ]P lx 9 lpx [
|
||||
]P
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
Public Sub Main()
|
||||
|
||||
Print Format("7.125", "00000.000")
|
||||
|
||||
End
|
||||
|
|
@ -0,0 +1 @@
|
|||
print sprintf("%09.3f", 7.125)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
// version 1.0.5-2
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val num = 7.125
|
||||
println("%09.3f".format(num))
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
. display %010.3f (57/8)
|
||||
000007.125
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
"%09.3f".fmt(7.125) //-->"00007.125"
|
||||
"%09.3e".fmt(7.125) //-->"7.125e+00"
|
||||
"%09.3g".fmt(7.125) //-->"000007.12"
|
||||
"%09d".fmt(7.125) //-->"000000007"
|
||||
"%09,d".fmt(78901.125)//-->"00078,901"
|
||||
Loading…
Add table
Add a link
Reference in a new issue