Non-decimal radices/Input: Difference between revisions

Factor
(Factor)
(Factor)
Line 103:
# value: 200</lang>
=={{header|Factor}}==
Bases from 2 to 16 are supported, through the generic base> word (see online docs [http://docs.factorcode.org/content/word-base__gt__,math.parser.html])
but 4 functions are defined for the most used cases:
( scratchpad ) "ff" 16 base> .
( scratchpad ) "ff" hex> . ! base 16
255
( scratchpad ) "f.f777" 16 baseoct> . ! base 8
15.9375511
( scratchpad ) "1111" bin> . ! base 2
15
( scratchpad ) "99" string>number . ! base 10
99
Note that these words are very simple : for example, here's oct> :
<lang factor>IN: math.parser
: oct> ( str -- n/f ) 8 base> ; inline</lang>
Also, fractions are handled transparently :
( scratchpad ) "1+F/2" hex> .
8+1/2
Hex floats are supported, anything else is taken as base 10 :
( scratchpad ) "ff.f" 16 basehex> .
255.9375
( scratchpad ) "11.1101" bin> .
11.1101
=={{header|Forth}}==
Arbitrary base 2-36 parsing is supported by the same mechanism as [[User Input#Forth|decimal parsing]]: set the user variable BASE to the desired base, then scan the number. There are two convenience words for setting the base to DECIMAL or HEX.
Anonymous user