Non-decimal radices/Input: Difference between revisions

→‎{{header|Phix}}: completely rewrote entry
(→‎{{header|Phix}}: completely rewrote entry)
Line 856:
 
=={{header|Phix}}==
There are four possible approaches here:<br>
<lang Phix>?scanf("1234","%d")
The entry-level routine for this is to_integer().<br>
?scanf("0b10101010","%d")
The to_number() routine copes with larger numbers, (decimal) fractions, and exponents.<br>
?scanf("#ABCD","%d")
The scanf() routine uses [prefixes and] to_number() internally, but has no explicit base parameter.<br>
?scanf("#FFFFFFFF","%f")
The sledgehammer routines are mpz_set_str() and mpfr_set_str(), with the latter even handling non-decimal fractions.
?scanf("0xFFFFFFFF","%f")
<!--<lang Phix>(phixonline)-->
?scanf("0o377","%o")</lang>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
{{out}}
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">to_integer</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1234"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 1234</span>
<pre>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">to_integer</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"10101010"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 170, 0 on failure</span>
{{1234}}
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">to_number</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"FFFFFFFF"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"?"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">16</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 4294967295.0, "?" on failure</span>
{{170}}
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"#FFFFFFFF"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%f"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- <nowiki>{{</nowiki>4294967295.0<nowiki>}}</nowiki>, {} on failure</span>
{{43981}}
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0o377"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%o"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- <nowiki>{{</nowiki>255<nowiki>}}</nowiki></span>
{{4294967295}}
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1234"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- <nowiki>{{</nowiki>1234<nowiki>}}</nowiki></span>
{{4294967295}}
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
{{255}}
<span style="color: #004080;">mpz</span> <span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
</pre>
<span style="color: #7060A8;">mpz_set_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"377"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)</span>
Note the need for %f (if you want to get an atom rather than an integer back), and double braces (it's a list of potentially several different possible result sets/interpretations), and that scanf() works best with a few literals (esp spaces but most certainly <b><i>not</i></b> radix prefixes) in the format string.
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- "255"</span>
Finally note that while you can use "#DEADBEEF" (without the quotes, ie a fairly big hex number) in a source code file, the compiler will choke on "DEADBEEF", and likewise so too will scanf(), and the only way round that is to insert the right prefix at the right place.
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">mpfr_set_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"110.01"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- "6.25" (which is correct in decimal)</span>
<!--</lang>-->
Aside: the ".0" is a sprint() artefact, to indicate "this is not a Phix 31/63-bit integer". scanf() can return multiple sets of answers. You can of course use mpz_fits_integer() and mpz_get_integer(), mpz_fits_atom() and mpz_get_atom(), mpfr_get_si(), or mpfr_get_d() to retrieve native values from gmp - the mpfr_fits_*() routines are not yet wrapped, give me a shout if they, or a more Phix-friendly version of them, are needed.
 
=={{header|PHP}}==
7,806

edits