Guest User

GForth's F.RDP modified to use the REPRESENT from DX-Forth and VFX.

a guest
Sep 28th, 2026
15
0
362 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.73 KB | Source Code | 0 0
  1. \ GForth's F.RDP modified to use the REPRESENT from DX-Forth and VFX.
  2. \
  3. \ No attempt was made to make it pretty - just the basic patches to
  4. \ remove code that was no longer needed and any additions necessary
  5. \ to make the GForth's test run.
  6. \
  7. \ dxf 2026-09-29
  8.  
  9. true \ enable/disable patch
  10.  
  11. dup constant ***ADD***
  12. 0= constant ***REMOVE***
  13.  
  14. ***ADD*** [IF]
  15.  
  16. MAX-PRECISION CONSTANT mc# \ min buffer size for DXF/VFX REPRESENT
  17.  
  18. \ Define Gforth words we don't necessarily have...
  19.  
  20. : endif postpone then ; immediate
  21. : \G postpone \ ; immediate
  22.  
  23. : 2tuck 2swap 2over ;
  24. : >= < 0= ;
  25. : u>= u< 0= ;
  26. : bounds over + swap ;
  27.  
  28. variable holdptr
  29. variable holdend
  30.  
  31. create holdbuf 100 chars allot
  32.  
  33. here constant holdbuf-end
  34.  
  35. : hold ( char -- ) \ core
  36. -1 chars holdptr +!
  37. holdptr @ dup holdbuf u< -17 and throw
  38. c! ;
  39.  
  40. : <# ( -- ) \ core less-number-sign
  41. holdbuf-end dup holdptr ! holdend ! ; <#
  42.  
  43. : #> ( xd -- addr u )
  44. 2drop holdptr @ holdend @ over - ;
  45.  
  46. : <<# ( -- )
  47. holdend @ holdptr @ - hold
  48. holdptr @ holdend ! ;
  49.  
  50. : #>> ( -- )
  51. holdend @ dup holdbuf-end u>= -11 and throw
  52. count chars bounds holdptr ! holdend ! ;
  53.  
  54. : sign ( n -- ) 0< IF [char] - hold THEN ;
  55.  
  56. : # ( ud1 -- ud2 )
  57. \ base @ ud/mod
  58. \ rot 9 over < IF [ char A char 9 - 1- ] Literal + THEN
  59. \ [char] 0 + hold ;
  60. base @ >r 0 r@ um/mod r> swap >r um/mod r>
  61. rot 9 over < if 7 + then [char] 0 + hold ;
  62.  
  63. : #s ( ud -- 0 0 ) BEGIN # 2dup or 0= UNTIL ;
  64.  
  65. : f2* ( r1 -- r2 ) 2.0e0 f* ;
  66.  
  67. [THEN]
  68.  
  69. \ f.rdp
  70.  
  71. : push-right ( c-addr u1 u2 cfill -- )
  72. \ move string at c-addr u1 right by u2 chars (without exceeding
  73. \ the original bound); fill the gap with cfill
  74. >r over min dup >r rot dup >r ( u1 u2 c-addr R: cfill u2 c-addr )
  75. dup 2swap /string cmove>
  76. r> r> r> fill ;
  77.  
  78. \ Gforth's version used locals. For debugging, this was easier.
  79.  
  80. fvariable rf
  81. 0 value c-addr 0 value ur 0 value nd 0 value up
  82. 0 value um1 0 value nexp 0 value fsign 0 value befored
  83. 0 value beforez 0 value beforep 0 value explen 0 value mantlen
  84.  
  85. ***ADD*** [IF] 0 value fixpt [THEN]
  86.  
  87. : f>buf-rdp-try ( f: rf c-addr ur nd up um1 -- um2 )
  88. to um1 to up to nd to ur to c-addr rf f!
  89. \ um1 is the mantissa length to try, um2 is the actual mantissa length
  90. c-addr ur um1 /string [char] 0 fill
  91. rf f@ c-addr um1 represent if to fsign to nexp
  92. nd nexp + up >= up 0= or
  93. ur nd - 1- dup to beforep fsign + nexp 0 max >= and
  94. [ ***ADD*** ] [IF] dup to fixpt [THEN] if
  95. \ fixed-point notation
  96. c-addr ur beforep nexp - dup to befored [char] 0 push-right
  97. [ ***REMOVE*** ] [IF]
  98. befored 1+ ur >= if \ <=1 digit left, will be pushed out by '.'
  99. rf f@ fabs f2* 0.1e nd s>d d>f f** f> if \ round last digit
  100. [char] 1 c-addr befored + 1- c!
  101. endif
  102. endif
  103. [THEN]
  104. c-addr beforep 1- befored min dup to beforez 0 max bl fill
  105. fsign if
  106. [char] - c-addr beforez 1- 0 max + c!
  107. endif
  108. c-addr ur beforep /string 1 [char] . push-right
  109. nexp nd +
  110. else \ exponential notation
  111. c-addr ur 1 /string 1 [char] . push-right
  112. fsign if
  113. c-addr ur 1 [char] - push-right
  114. endif
  115. nexp 1- s>d tuck dabs <<# #s rot sign [char] E hold #> to explen
  116. ur explen - 1- fsign + to mantlen
  117. mantlen 0< if \ exponent too large
  118. drop c-addr ur [char] * fill
  119. else
  120. c-addr ur + 0 explen negate /string move
  121. endif
  122. #>> mantlen
  123. endif
  124. else \ inf or nan
  125. \ don't rely on REPRESENT result
  126. 2drop
  127. [ ***REMOVE*** ] [IF]
  128. rf f@ f0< if s" -Inf" else rf f@ f0>= if s" Inf" else s" NaN" endif endif
  129. c-addr ur rot umin dup >r move c-addr ur r> /string blank
  130. [THEN]
  131. ur
  132. endif
  133. [ ***ADD*** ] [IF] fixpt 0= if [THEN]
  134. 1 max
  135. [ ***ADD*** ] [IF] then [THEN]
  136. ur min ;
  137.  
  138. : f>buf-rdp ( rf c-addr +nr +nd +np -- ) \ gforth
  139. \G Convert @i{rf} into a string at @i{c-addr nr}. The conversion
  140. \G rules and the meanings of @i{nr nd np} are the same as for
  141. \G @code{f.rdp}.
  142. \ first, get the mantissa length, then convert for real. The
  143. \ mantissa length is wrong in a few cases because of different
  144. \ rounding; In most cases this does not matter, because the
  145. \ mantissa is shorter than expected and the final digits are 0;
  146. \ but in a few cases the mantissa gets longer. Then it is
  147. \ conceivable that you will see a result that is rounded too much.
  148. \ However, I have not been able to construct an example where this
  149. \ leads to an unexpected result.
  150. swap 0 max swap 0 max
  151. fdup 2over 2over 2 pick f>buf-rdp-try f>buf-rdp-try drop ;
  152.  
  153. : f>str-rdp ( rf +nr +nd +np -- c-addr nr ) \ gforth
  154. \G Convert @i{rf} into a string at @i{c-addr nr}. The conversion
  155. \G rules and the meanings of @i{nr +nd np} are the same as for
  156. \G @code{f.rdp}. The result in in the pictured numeric output buffer
  157. \G and will be destroyed by anything destroying that buffer.
  158. rot holdptr @ 1-
  159. [ ***ADD*** ] [IF] over mc# max chars - [THEN]
  160. 0 rot negate /string ( rf +nd np c-addr nr )
  161. over holdbuf u< -17 and throw
  162. 2tuck 2>r f>buf-rdp 2r> ;
  163.  
  164. : f.rdp ( rf +nr +nd +np -- ) \ gforth
  165. \G Print float @i{rf} formatted. The total width of the output is
  166. \G @i{nr}. For fixed-point notation, the number of digits after the
  167. \G decimal point is @i{+nd} and the minimum number of significant
  168. \G digits is @i{np}. @code{Set-precision} has no effect on
  169. \G @code{f.rdp}. Fixed-point notation is used if the number of
  170. \G siginicant digits would be at least @i{np} and if the number of
  171. \G digits before the decimal point would fit. If fixed-point notation
  172. \G is not used, exponential notation is used, and if that does not
  173. \G fit, asterisks are printed. We recommend using @i{nr}>=7 to avoid
  174. \G the risk of numbers not fitting at all. We recommend
  175. \G @i{nr}>=@i{np}+5 to avoid cases where @code{f.rdp} switches to
  176. \G exponential notation because fixed-point notation would have too
  177. \G few significant digits, yet exponential notation offers fewer
  178. \G significant digits. We recommend @i{nr}>=@i{nd}+2, if you want to
  179. \G have fixed-point notation for some numbers; the smaller the value
  180. \G of @i{np}, the more cases are shown in fixed-point notation (cases
  181. \G where few or no significant digits remain in fixed-point notation).
  182. \G We recommend @i{np}>@i{nr}, if you want to have exponential
  183. \G notation for all numbers.
  184. f>str-rdp type ;
  185.  
  186. 1 [if]
  187. : testx ( rf ur nd up -- )
  188. [char] | emit f.rdp ;
  189.  
  190. : test ( -- )
  191. -0.123456789123456789e-20
  192. 40 0 ?do
  193. cr
  194. fdup 7 3 1 testx
  195. fdup 7 3 4 testx
  196. fdup 7 3 0 testx
  197. fdup 7 7 1 testx
  198. fdup 7 5 1 testx
  199. fdup 7 0 2 testx
  200. fdup 5 2 1 testx
  201. fdup 4 2 1 testx
  202. fdup 18 8 5 testx
  203. [char] | emit
  204. 10e f*
  205. loop fdrop ;
  206. [then]
  207.  
  208. 1 [IF]
  209.  
  210. : (foo) ( r -- )
  211. cr 8 spaces
  212. fdup
  213. 12 5 13 f.rdp
  214. 10 spaces
  215. fdup
  216. 8 5 0 f.rdp
  217. 8 spaces
  218. 7 0 0 f.rdp
  219. ;
  220.  
  221. : foo
  222. -1.23457e-8 (foo)
  223. -1.23457e (foo)
  224. 0.9e (foo)
  225. 0.4e (foo)
  226. 0e 0e f/ (foo)
  227. -0e 0e f/ (foo)
  228. 1e 0e f/ (foo)
  229. -1e 0e f/ (foo)
  230. ;
  231.  
  232. [THEN]
  233.  
  234. 0 [IF] \ test results
  235.  
  236. |-1.E-21|-1.E-21| 0.000|-1.E-21|-1.E-21|-1.E-21|*****|****|-1.23456789123E-21|
  237. |-1.E-20|-1.E-20| 0.000|-1.E-20|-1.E-20|-1.E-20|*****|****|-1.23456789123E-20|
  238. |-1.E-19|-1.E-19| 0.000|-1.E-19|-1.E-19|-1.E-19|*****|****|-1.23456789123E-19|
  239. |-1.E-18|-1.E-18| 0.000|-1.E-18|-1.E-18|-1.E-18|*****|****|-1.23456789123E-18|
  240. |-1.E-17|-1.E-17| 0.000|-1.E-17|-1.E-17|-1.E-17|*****|****|-1.23456789123E-17|
  241. |-1.E-16|-1.E-16| 0.000|-1.E-16|-1.E-16|-1.E-16|*****|****|-1.23456789123E-16|
  242. |-1.E-15|-1.E-15| 0.000|-1.E-15|-1.E-15|-1.E-15|*****|****|-1.23456789123E-15|
  243. |-1.E-14|-1.E-14| 0.000|-1.E-14|-1.E-14|-1.E-14|*****|****|-1.23456789123E-14|
  244. |-1.E-13|-1.E-13| 0.000|-1.E-13|-1.E-13|-1.E-13|*****|****|-1.23456789123E-13|
  245. |-1.E-12|-1.E-12| 0.000|-1.E-12|-1.E-12|-1.E-12|*****|****|-1.23456789123E-12|
  246. |-1.E-11|-1.E-11| 0.000|-1.E-11|-1.E-11|-1.E-11|*****|****|-1.23456789123E-11|
  247. |-1.E-10|-1.E-10| 0.000|-1.E-10|-1.E-10|-1.E-10|*****|****|-1.23456789123E-10|
  248. |-1.2E-9|-1.2E-9| 0.000|-1.2E-9|-1.2E-9|-1.2E-9|-1E-9|****|-1.234567891235E-9|
  249. |-1.2E-8|-1.2E-8| 0.000|-1.2E-8|-1.2E-8|-1.2E-8|-1E-8|****|-1.234567891235E-8|
  250. |-1.2E-7|-1.2E-7| 0.000|-1.2E-7|-1.2E-7|-1.2E-7|-1E-7|****|-1.234567891235E-7|
  251. |-1.2E-6|-1.2E-6| 0.000|-1.2E-6|-1.2E-6|-1.2E-6|-1E-6|****|-1.234567891235E-6|
  252. |-1.2E-5|-1.2E-5| 0.000|-1.2E-5|-.00001|-1.2E-5|-1E-5|****|-1.234567891235E-5|
  253. |-1.2E-4|-1.2E-4| 0.000|-1.2E-4|-.00012|-1.2E-4|-1E-4|****| -0.00012346|
  254. | -0.001|-1.2E-3| -0.001|-1.2E-3|-.00123|-1.2E-3|-1E-3|****| -0.00123457|
  255. | -0.012|-1.2E-2| -0.012|-1.2E-2|-.01235|-1.2E-2|-0.01|-.01| -0.01234568|
  256. | -0.123|-1.2E-1| -0.123|-1.2E-1|-.12346|-1.2E-1|-0.12|-.12| -0.12345679|
  257. | -1.235| -1.235| -1.235|-1.23E0|-1.23E0|-1.23E0|-1.23|-1E0| -1.23456789|
  258. |-12.346|-12.346|-12.346|-1.23E1|-1.23E1| -12.|-1.E1|-1E1| -12.34567891|
  259. |-1.23E2|-1.23E2|-1.23E2|-1.23E2|-1.23E2| -123.|-1.E2|-1E2| -123.45678912|
  260. |-1.23E3|-1.23E3|-1.23E3|-1.23E3|-1.23E3| -1235.|-1.E3|-1E3| -1234.56789123|
  261. |-1.23E4|-1.23E4|-1.23E4|-1.23E4|-1.23E4|-12346.|-1.E4|-1E4| -12345.67891235|
  262. |-1.23E5|-1.23E5|-1.23E5|-1.23E5|-1.23E5|-1.23E5|-1.E5|-1E5| -123456.78912346|
  263. |-1.23E6|-1.23E6|-1.23E6|-1.23E6|-1.23E6|-1.23E6|-1.E6|-1E6| -1234567.89123457|
  264. |-1.23E7|-1.23E7|-1.23E7|-1.23E7|-1.23E7|-1.23E7|-1.E7|-1E7|-12345678.91234570|
  265. |-1.23E8|-1.23E8|-1.23E8|-1.23E8|-1.23E8|-1.23E8|-1.E8|-1E8|-1.2345678912346E8|
  266. |-1.23E9|-1.23E9|-1.23E9|-1.23E9|-1.23E9|-1.23E9|-1.E9|-1E9|-1.2345678912346E9|
  267. |-1.2E10|-1.2E10|-1.2E10|-1.2E10|-1.2E10|-1.2E10|-1E10|****|-1.234567891235E10|
  268. |-1.2E11|-1.2E11|-1.2E11|-1.2E11|-1.2E11|-1.2E11|-1E11|****|-1.234567891235E11|
  269. |-1.2E12|-1.2E12|-1.2E12|-1.2E12|-1.2E12|-1.2E12|-1E12|****|-1.234567891235E12|
  270. |-1.2E13|-1.2E13|-1.2E13|-1.2E13|-1.2E13|-1.2E13|-1E13|****|-1.234567891235E13|
  271. |-1.2E14|-1.2E14|-1.2E14|-1.2E14|-1.2E14|-1.2E14|-1E14|****|-1.234567891235E14|
  272. |-1.2E15|-1.2E15|-1.2E15|-1.2E15|-1.2E15|-1.2E15|-1E15|****|-1.234567891235E15|
  273. |-1.2E16|-1.2E16|-1.2E16|-1.2E16|-1.2E16|-1.2E16|-1E16|****|-1.234567891235E16|
  274. |-1.2E17|-1.2E17|-1.2E17|-1.2E17|-1.2E17|-1.2E17|-1E17|****|-1.234567891235E17|
  275. |-1.2E18|-1.2E18|-1.2E18|-1.2E18|-1.2E18|-1.2E18|-1E18|****|-1.234567891235E18| ok
  276.  
  277.  
  278. -1.234570E-8 0.00000 0.
  279. -1.2345700E0 -1.23457 -1.
  280. 9.0000000E-1 0.90000 1.
  281. 4.0000000E-1 0.40000 0.
  282. -NAN -NAN -NAN
  283. -NAN -NAN -NAN
  284. +INF +INF +INF
  285. -INF -INF -INF ok
  286.  
  287. [THEN]
  288.  
Advertisement
Add Comment
Please, Sign In to add comment