Imaginary base numbers: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Haskell}}: Applied Ormolu, tidied, print -> putStrln, updated output.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 314:
{{trans|Python}}
<
V denom = c.real * c.real + c.imag * c.imag
R Complex(c.real / denom, -c.imag / denom)
Line 412:
print(‘#8 -> #8 -> #8’.format(c1, qi, c2))
print(‘done’)</
{{out}}
Line 454:
=={{header|C}}==
{{trans|C++}}
<
#include <stdio.h>
#include <string.h>
Line 722:
return 0;
}</
{{out}}
<pre>( 1 + 0i) -> 1 -> ( 1 + 0i) ( -1 + -0i) -> 103 -> ( -1 + 0i)
Line 759:
=={{header|C sharp|C#}}==
<
using System.Linq;
using System.Text;
Line 935:
}
}
}</
{{out}}
<pre> 1 -> 1 -> 1 -1 -> 103 -> -1
Line 973:
=={{header|C++}}==
{{trans|C#}}
<
#include <complex>
#include <iomanip>
Line 1,117:
return 0;
}</
{{out}}
<pre> (1,0) -> 1 -> (1,0) (-1,-0) -> 103 -> (-1,0)
Line 1,155:
=={{header|D}}==
{{trans|Kotlin}}
<
import std.array;
import std.complex;
Line 1,307:
writefln("%4si -> %8s -> %4si", c1.im, qi, c2.im);
}
}</
{{out}}
<pre> 1 -> 1 -> 1 -1 -> 103 -> -1
Line 1,346:
{{trans|Kotlin}}
... though a bit shorter as Go has support for complex numbers built into the language.
<
import (
Line 1,492:
fmt.Printf("%3.0fi -> %8s -> %3.0fi\n", imag(c1), qi, imag(c2))
}
}</
{{out}}
Line 1,532:
=={{header|Haskell}}==
<
import Data.Complex (Complex (..), imagPart, realPart)
import Data.List (delete, elemIndex)
Line 1,620:
main =
putStrLn (fromComplexToQI (35 :+ 23))
>> print (fromQItoComplex "10.2" base)</
{{out}}
<pre>121003.2
Line 1,632:
Implementation:
<syntaxhighlight lang="j">
ibdec=: {{
0j2 ibdec y
Line 1,652:
frac,~(}.~0 i.~_1}.'0'=]) }:,hfd|:0 1|."0 1 seq re,im
}}"0
</syntaxhighlight>
This ibdec can decode numbers from complex bases up to 0j6, but this ibenc can only represent digits in complex bases up to 0j4.
Line 1,658:
Examples:
<syntaxhighlight lang="j">
(ibenc i:16),.' ',.ibenc j.i:16
1030000 2000
Line 1,700:
0j4 ibdec 0j4 ibenc 42
42
</syntaxhighlight>
=={{header|Java}}==
{{trans|Kotlin}}
<
private static class Complex {
private Double real, imag;
Line 1,881:
}
}
}</
{{out}}
<pre> 1 -> 1 -> 1 -1 -> 103 -> -1
Line 1,919:
=={{header|Julia}}==
{{trans|C#}}
<
function inbase4(charvec::Vector)
Line 2,087:
testquim()
</
1 -> 1 -> 1 -1 -> 103 -> -1
2 -> 2 -> 2 -2 -> 102 -> -2
Line 2,127:
As the JDK lacks a complex number class, I've included a very basic one in the program.
<
import kotlin.math.ceil
Line 2,272:
println(fmt.format(c1, qi, c2))
}
}</
{{out}}
Line 2,313:
=={{header|Modula-2}}==
{{trans|C#}}
<
FROM FormatString IMPORT FormatString;
FROM RealMath IMPORT round;
Line 2,649:
ReadChar
END ImaginaryBase.</
{{out}}
<pre>1 -> 1 -> 1 -1 -> 103 -> -1
Line 2,688:
{{trans|Kotlin}}
This is a fairly faithful translation of the Kotlin program except that we had not to define a Complex type as Nim provides the module “complex” in its standard library. We had only to define a function “toString” for the “Complex[float]” type, function to use in place of “$” in order to get a more pleasant output.
<
const
Line 2,822:
qi = c1.toQuaterImaginary
c2 = qi.toComplex
echo fmt"{c1.toString:>4s} → {qi:>8s} → {c2.toString:>4s}"</
{{out}}
Line 2,862:
{{trans|Raku}}
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 2,943:
say '';
say 'base( 6i): 31432.6219135802-2898.5266203704*i => ' .
base_c(31432.6219135802-2898.5266203704*i, 0+6*i, -3);</
{{out}}
<pre>base( 2i): 0 => 0 => 0
Line 2,968:
=={{header|Phix}}==
{{trans|Sidef}}
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">complex</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 3,097:
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
Matches the output of Sidef and Raku, except for the final line:
Line 3,115:
=={{header|Python}}==
{{trans|C++}}
<
import re
Line 3,215:
print "done"
</syntaxhighlight>
{{out}}
<pre> (1+0j) -> 1 -> (1+0j) (-1-0j) -> 103 -> (-1+0j)
Line 3,260:
Implements minimum, extra kudos and stretch goals.
<syntaxhighlight lang="raku"
return '0' unless $num;
my $value = $num;
Line 3,315:
printf "%33s.&base\(%2si\) = %-11s : %13s.&parse-base\(%2si\) = %s\n",
$v, $r.im, $ibase, "'$ibase'", $r.im, $ibase.&parse-base($r).round(1e-10).narrow;
}</
{{out}}
<pre> 0.&base( 2i) = 0 : '0'.&parse-base( 2i) = 0
Line 3,347:
Doing pretty much the same tests as the explicit version.
<syntaxhighlight lang="raku"
# TESTING
Line 3,360:
printf "%33s.&to-base\(%3si\) = %-11s : %13s.&from-base\(%3si\) = %s\n",
$v, $r.im, $ibase, "'$ibase'", $r.im, $ibase.&from-base($r).round(1e-10).narrow;
}</
{{out}}
<pre> 0.&to-base( 2i) = 0 : '0'.&from-base( 2i) = 0
Line 3,386:
{{works with|Ruby|2.3}}
'''The Functions'''
<
def base2i_decode(qi)
Line 3,423:
value.concat(odd ? '.2' : '.0') if frac
return value
end</
'''The Task'''
<
class Integer
Line 3,470:
puts
end
end</
{{out}}
Conversions given in the task.
Line 3,527:
=={{header|Sidef}}==
{{trans|Raku}}
<
num || return '0'
Line 3,597:
printf("base(%20s, %2si) = %-10s : parse_base(%12s, %2si) = %s\n",
v, r.im, ibase, "'#{ibase}'", r.im, parse_base(ibase, r).round(-8))
})</
{{out}}
<pre>
Line 3,623:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Module Module1
Line 3,810:
End Sub
End Module</
{{out}}
<pre> 1 -> 1 -> 1 -1 -> 103 -> -1
Line 3,850:
{{libheader|Wren-complex}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 3,961:
c2 = qi.toComplex
Fmt.print(fmt, imagOnly.call(c1), qi, imagOnly.call(c2))
}</
{{out}}
|