Jump to content

Imaginary base numbers: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Haskell}}: Applied Ormolu, tidied, print -> putStrln, updated output.)
m (syntax highlighting fixup automation)
Line 314:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F inv(c)
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’)</langsyntaxhighlight>
 
{{out}}
Line 454:
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
#include <string.h>
Line 722:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>( 1 + 0i) -> 1 -> ( 1 + 0i) ( -1 + -0i) -> 103 -> ( -1 + 0i)
Line 759:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Text;
Line 935:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 1 -> 1 -> 1 -1 -> 103 -> -1
Line 973:
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <complex>
#include <iomanip>
Line 1,117:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> (1,0) -> 1 -> (1,0) (-1,-0) -> 103 -> (-1,0)
Line 1,155:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.algorithm;
import std.array;
import std.complex;
Line 1,307:
writefln("%4si -> %8s -> %4si", c1.im, qi, c2.im);
}
}</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,492:
fmt.Printf("%3.0fi -> %8s -> %3.0fi\n", imag(c1), qi, imag(c2))
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,532:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Data.Char (chr, digitToInt, intToDigit, isDigit, ord)
import Data.Complex (Complex (..), imagPart, realPart)
import Data.List (delete, elemIndex)
Line 1,620:
main =
putStrLn (fromComplexToQI (35 :+ 23))
>> print (fromQItoComplex "10.2" base)</langsyntaxhighlight>
{{out}}
<pre>121003.2
Line 1,632:
Implementation:
 
<syntaxhighlight lang="j">
<lang J>
ibdec=: {{
0j2 ibdec y
Line 1,652:
frac,~(}.~0 i.~_1}.'0'=]) }:,hfd|:0 1|."0 1 seq re,im
}}"0
</syntaxhighlight>
</lang>
 
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">
<lang J>
(ibenc i:16),.' ',.ibenc j.i:16
1030000 2000
Line 1,700:
0j4 ibdec 0j4 ibenc 42
42
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight Javalang="java">public class ImaginaryBaseNumber {
private static class Complex {
private Double real, imag;
Line 1,881:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 1 -> 1 -> 1 -1 -> 103 -> -1
Line 1,919:
=={{header|Julia}}==
{{trans|C#}}
<langsyntaxhighlight lang="julia">import Base.show, Base.parse, Base.+, Base.-, Base.*, Base./, Base.^
 
function inbase4(charvec::Vector)
Line 2,087:
 
testquim()
</langsyntaxhighlight>{{output}}<pre>
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.
<langsyntaxhighlight lang="scala">// version 1.2.10
 
import kotlin.math.ceil
Line 2,272:
println(fmt.format(c1, qi, c2))
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,313:
=={{header|Modula-2}}==
{{trans|C#}}
<langsyntaxhighlight lang="modula2">MODULE ImaginaryBase;
FROM FormatString IMPORT FormatString;
FROM RealMath IMPORT round;
Line 2,649:
 
ReadChar
END ImaginaryBase.</langsyntaxhighlight>
{{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.
<langsyntaxhighlight Nimlang="nim">import algorithm, complex, math, strformat, strutils
 
const
Line 2,822:
qi = c1.toQuaterImaginary
c2 = qi.toComplex
echo fmt"{c1.toString:>4s} → {qi:>8s} → {c2.toString:>4s}"</langsyntaxhighlight>
 
{{out}}
Line 2,862:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
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);</langsyntaxhighlight>
{{out}}
<pre>base( 2i): 0 => 0 => 0
Line 2,968:
=={{header|Phix}}==
{{trans|Sidef}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
Matches the output of Sidef and Raku, except for the final line:
Line 3,115:
=={{header|Python}}==
{{trans|C++}}
<langsyntaxhighlight lang="python">import math
import re
 
Line 3,215:
 
print "done"
</syntaxhighlight>
</lang>
{{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" perl6line>multi sub base ( Real $num, Int $radix where -37 < * < -1, :$precision = -15 ) {
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;
}</langsyntaxhighlight>
{{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" perl6line>use Base::Any;
 
# 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;
}</langsyntaxhighlight>
{{out}}
<pre> 0.&to-base( 2i) = 0 : '0'.&from-base( 2i) = 0
Line 3,386:
{{works with|Ruby|2.3}}
'''The Functions'''
<langsyntaxhighlight lang="ruby"># Convert a quarter-imaginary base value (as a string) into a base 10 Gaussian integer.
 
def base2i_decode(qi)
Line 3,423:
value.concat(odd ? '.2' : '.0') if frac
return value
end</langsyntaxhighlight>
'''The Task'''
<langsyntaxhighlight lang="ruby"># Extend class Integer with a string conveter.
 
class Integer
Line 3,470:
puts
end
end</langsyntaxhighlight>
{{out}}
Conversions given in the task.
Line 3,527:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func base (Number num, Number radix { _ ~~ (-36 .. -2) }, precision = -15) -> String {
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))
})</langsyntaxhighlight>
{{out}}
<pre>
Line 3,623:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Text
 
Module Module1
Line 3,810:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre> 1 -> 1 -> 1 -1 -> 103 -> -1
Line 3,850:
{{libheader|Wren-complex}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/complex" for Complex
import "/fmt" for Fmt
 
Line 3,961:
c2 = qi.toComplex
Fmt.print(fmt, imagOnly.call(c1), qi, imagOnly.call(c2))
}</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.