Imaginary base numbers: Difference between revisions

Added FreeBASIC
m (Cleanup sections.)
(Added FreeBASIC)
 
(4 intermediate revisions by 4 users not shown)
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)
 
V QuaterImaginary_twoI = Complex(0, 2)
V QuaterImaginary_invTwoI = inv(QuaterImaginary_twoI)
 
T QuaterImaginary
:twoI = Complex(0, 2)
:invTwoI = inv(.:twoI)
 
String b2i
 
Line 338:
I k > 0
sum += prod * k
prod *= QuaterImaginary_twoI.:twoI
I pointPos != -1
prod = QuaterImaginary_invTwoI.:invTwoI
L(j) posLen + 1 .< .b2i.len
V k = Int(.b2i[j])
I k > 0
sum += prod * k
prod *= QuaterImaginary_invTwoI.:invTwoI
R sum
 
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,342:
15i -> 102000.2 -> 15i -15i -> 2010.2 -> -15i
16i -> 102000.0 -> 16i -16i -> 2000.0 -> -16i</pre>
 
=={{header|FreeBASIC}}==
{{trans|Modula-2}}
<syntaxhighlight lang="vbnet">#define ceil(x) (-((-x*2.0-0.5) Shr 1))
 
Type Complex
real As Double
imag As Double
End Type
 
Type QuaterImaginary
b2i As String
End Type
 
Dim Shared As Complex c1, c2
 
Function StrReverse(Byval txt As String) As String
Dim result As String
For i As Integer = Len(txt) To 1 Step -1
result &= Mid(txt, i, 1)
Next i
Return result
End Function
 
Function ToChar(n As Integer) As String
Return Chr(n + Asc("0"))
End Function
 
Function ComplexMul(lhs As Complex, rhs As Complex) As Complex
Dim As Complex result
result.real = rhs.real * lhs.real - rhs.imag * lhs.imag
result.imag = rhs.real * lhs.imag + rhs.imag * lhs.real
Return result
End Function
 
Function ComplexMulR(lhs As Complex, rhs As Double) As Complex
Dim As Complex result
result.real = lhs.real * rhs
result.imag = lhs.imag * rhs
Return result
End Function
 
Function ComplexInv(c As Complex) As Complex
Dim As Double denom
Dim As Complex result
denom = c.real * c.real + c.imag * c.imag
result.real = c.real / denom
result.imag = -c.imag / denom
Return result
End Function
 
Function ComplexDiv(lhs As Complex, rhs As Complex) As Complex
Return ComplexMul(lhs, ComplexInv(rhs))
End Function
 
Function ComplexNeg(c As Complex) As Complex
Dim As Complex result
result.real = -c.real
result.imag = -c.imag
Return result
End Function
 
Function ComplexSum(lhs As Complex, rhs As Complex) As Complex
Dim As Complex result
result.real = lhs.real + rhs.real
result.imag = lhs.imag + rhs.imag
Return result
End Function
 
Function ToQuaterImaginary(c As Complex) As QuaterImaginary
Dim As Integer re, im, fi, rem_, index
Dim As Double f
Dim As Complex t
Dim As QuaterImaginary result
Dim As String sb
re = Int(c.real)
im = Int(c.imag)
fi = -1
While re <> 0
rem_ = (re Mod -4)
re = re \ (-4)
If rem_ < 0 Then
rem_ = 4 + rem_
re += 1
End If
sb &= ToChar(rem_) & "0"
Wend
If im <> 0 Then
t = ComplexDiv(Type<Complex>(0.0, c.imag), Type<Complex>(0.0, 2.0))
f = t.real
im = Ceil(f)
f = -4.0 * (f - Cdbl(im))
index = 1
While im <> 0
rem_ = im Mod -4
im \= -4
If rem_ < 0 Then
rem_ = 4 + rem_
im += 1
End If
If index < Len(sb) Then
Mid(sb, index + 1, 1) = ToChar(rem_)
Else
sb &= "0" & ToChar(rem_)
End If
index += 2
Wend
fi = Int(f)
End If
sb = StrReverse(sb)
If fi <> -1 Then sb &= "." & ToChar(fi)
sb = Ltrim(sb, "0")
If Left(sb, 1) = "." Then sb = "0" & sb
result.b2i = sb
Return result
End Function
 
Function ToComplex(qi As QuaterImaginary) As Complex
Dim As Integer j, pointPos, posLen, b2iLen
Dim As Double k
Dim As Complex sum, prod
pointPos = Instr(qi.b2i, ".")
posLen = Iif(pointPos = 0, Len(qi.b2i), pointPos - 1)
sum.real = 0.0
sum.imag = 0.0
prod.real = 1.0
prod.imag = 0.0
For j = 0 To posLen - 1
k = Val(Mid(qi.b2i, posLen - j, 1))
If k > 0.0 Then sum = ComplexSum(sum, ComplexMulR(prod, k))
prod = ComplexMul(prod, Type<Complex>(0.0, 2.0))
Next
If pointPos <> 0 Then
prod = ComplexInv(Type<Complex>(0.0, 2.0))
b2iLen = Len(qi.b2i)
For j = posLen + 1 To b2iLen - 1
k = Val(Mid(qi.b2i, j + 1, 1))
If k > 0.0 Then sum = ComplexSum(sum, ComplexMulR(prod, k))
prod = ComplexMul(prod, ComplexInv(Type<Complex>(0.0, 2.0)))
Next
End If
Return sum
End Function
 
Dim As QuaterImaginary qi
Dim As Integer i
For i = 1 To 16
c1.real = Cdbl(i)
c1.imag = 0.0
qi = ToQuaterImaginary(c1)
c2 = ToComplex(qi)
Print c1.real; "i -> "; qi.b2i; " -> "; c2.real; "i";
c1 = ComplexNeg(c1)
qi = ToQuaterImaginary(c1)
c2 = ToComplex(qi)
Print c1.real; "i -> "; qi.b2i; " -> "; c2.real; "i"
Next
Print
For i = 1 To 16
c1.real = 0.0
c1.imag = Cdbl(i)
qi = ToQuaterImaginary(c1)
c2 = ToComplex(qi)
Print c1.imag; "i -> "; qi.b2i; " -> "; c2.imag; "i";
c1 = ComplexNeg(c1)
qi = ToQuaterImaginary(c1)
c2 = ToComplex(qi)
Print c1.imag; "i -> "; qi.b2i; " -> "; c2.imag; "i"
Next
 
Sleep</syntaxhighlight>
{{out}}
<pre>Same as Modula-2 entry.</pre>
 
=={{header|Go}}==
{{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 ⟶ 1,665:
fmt.Printf("%3.0fi -> %8s -> %3.0fi\n", imag(c1), qi, imag(c2))
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,532 ⟶ 1,705:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Data.Char (chr, digitToInt, intToDigit, isDigit, ord)
import Data.Complex (Complex (..), imagPart, realPart)
import Data.List (elemIndexdelete, deleteelemIndex)
import Data.Maybe (fromMaybe)
 
base :: Complex Float
base = 0 :+ 2
 
quotRemPositive :: Int -> Int -> (Int, Int)
quotRemPositive a b
Line 1,547 ⟶ 1,719:
where
(q, r) = quotRem a b
 
digitToIntQI :: Char -> Int
digitToIntQI c
| isDigit c = digitToInt c
| otherwise = ord c - ord 'a' + 10
 
shiftRight :: String -> String
shiftRight n
| l == '0' = h
| otherwise = h ++<> "('."' ++: [l])
where
(l, h) = (last n, init n)
 
intToDigitQI :: Int -> Char
intToDigitQI i
| i `elem` [0 .. 9] = intToDigit i
| otherwise = chr (i - 10 + ord 'a')
 
fromQItoComplex :: String -> Complex Float -> Complex Float
fromQItoComplex num b =
let dot = fromMaybe (length num) (elemIndex '.' num)
in fst $
foldl
( \(a, indx) x ->
( a + fromIntegral (digitToIntQI x) * (b ^^ (dot - indx)), indx + 1))
* (0,b 1^^ (dot - indx)),
(delete '.' num) indx + 1
)
)
(0, 1)
(delete '.' num)
 
euclidEr :: Int -> Int -> [Int] -> [Int]
euclidEr a b l
| a == 0 = l
| otherwise =
let (q, r) = quotRemPositive a b
in euclidEr q b (0 : r : l)
 
fromIntToQI :: Int -> [Int]
fromIntToQI 0 = [0]
fromIntToQI x = tail (euclidEr x (floor $ realPart (base ^^ 2)) [])
tail
( euclidEr
x
(floor $ realPart (base ^^ 2))
[]
)
 
getCuid :: Complex Int -> Int
getCuid c = imagPart c * floor (imagPart (-base))
 
qizip :: Complex Int -> [Int]
qizip c =
let (r, i) =
let (r, i) = (fromIntToQI (realPart c) ++ [0], fromIntToQI (getCuid c))
in let m = min (length r)fromIntToQI (lengthrealPart ic) <> [0],
in take (length r - m)fromIntToQI r(getCuid ++c)
take (length i - m) i ++
in let m = reversemin (zipWith (+) (take m (reverselength r)) (take m (reverselength i)))
in take (length r - m) r
<> take (length i - m) i
<> reverse
( zipWith
(+)
(take m (reverse r))
(take m (reverse i))
)
 
fromComplexToQI :: Complex Int -> String
fromComplexToQI = shiftRight . fmap intToDigitQI . qizip
 
main :: IO ()
main =
main = print (fromComplexToQI (35 :+ 23)) >> print (fromQItoComplex "10.2" base)</lang>
putStrLn (fromComplexToQI (35 :+ 23))
>> print (fromQItoComplex "10.2" base)</syntaxhighlight>
{{out}}
<pre>"121003.2"
0.0 :+ 1.0</pre>
</pre>
With base = 8i (you may choose any base):
<pre>3z.8
"3z.8"
0.0 :+ 7.75</pre>
 
Line 1,615 ⟶ 1,805:
Implementation:
 
<syntaxhighlight lang="j">
<lang J>
ibdec=: {{
0j2 ibdec y
Line 1,635 ⟶ 1,825:
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,641 ⟶ 1,831:
Examples:
 
<syntaxhighlight lang="j">
<lang J>
(ibenc i:16),.' ',.ibenc j.i:16
1030000 2000
Line 1,683 ⟶ 1,873:
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,864 ⟶ 2,054:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 1 -> 1 -> 1 -1 -> 103 -> -1
Line 1,902 ⟶ 2,092:
=={{header|Julia}}==
{{trans|C#}}
<langsyntaxhighlight lang="julia">import Base.show, Base.parse, Base.+, Base.-, Base.*, Base./, Base.^
 
function inbase4(charvec::Vector)
Line 2,070 ⟶ 2,260:
 
testquim()
</langsyntaxhighlight>{{output}}<pre>
1 -> 1 -> 1 -1 -> 103 -> -1
2 -> 2 -> 2 -2 -> 102 -> -2
Line 2,110 ⟶ 2,300:
 
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,255 ⟶ 2,445:
println(fmt.format(c1, qi, c2))
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,296 ⟶ 2,486:
=={{header|Modula-2}}==
{{trans|C#}}
<langsyntaxhighlight lang="modula2">MODULE ImaginaryBase;
FROM FormatString IMPORT FormatString;
FROM RealMath IMPORT round;
Line 2,632 ⟶ 2,822:
 
ReadChar
END ImaginaryBase.</langsyntaxhighlight>
{{out}}
<pre>1 -> 1 -> 1 -1 -> 103 -> -1
Line 2,671 ⟶ 2,861:
{{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,805 ⟶ 2,995:
qi = c1.toQuaterImaginary
c2 = qi.toComplex
echo fmt"{c1.toString:>4s} → {qi:>8s} → {c2.toString:>4s}"</langsyntaxhighlight>
 
{{out}}
Line 2,845 ⟶ 3,035:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 2,926 ⟶ 3,116:
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,951 ⟶ 3,141:
=={{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,080 ⟶ 3,270:
<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,098 ⟶ 3,288:
=={{header|Python}}==
{{trans|C++}}
<langsyntaxhighlight lang="python">import math
import re
 
Line 3,198 ⟶ 3,388:
 
print "done"
</syntaxhighlight>
</lang>
{{out}}
<pre> (1+0j) -> 1 -> (1+0j) (-1-0j) -> 103 -> (-1+0j)
Line 3,243 ⟶ 3,433:
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,298 ⟶ 3,488:
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,330 ⟶ 3,520:
Doing pretty much the same tests as the explicit version.
 
<syntaxhighlight lang="raku" perl6line>use Base::Any;
 
# TESTING
Line 3,343 ⟶ 3,533:
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,369 ⟶ 3,559:
{{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,406 ⟶ 3,596:
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,453 ⟶ 3,643:
puts
end
end</langsyntaxhighlight>
{{out}}
Conversions given in the task.
Line 3,510 ⟶ 3,700:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func base (Number num, Number radix { _ ~~ (-36 .. -2) }, precision = -15) -> String {
num || return '0'
 
Line 3,580 ⟶ 3,770:
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,606 ⟶ 3,796:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Text
 
Module Module1
Line 3,793 ⟶ 3,983:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre> 1 -> 1 -> 1 -1 -> 103 -> -1
Line 3,833 ⟶ 4,023:
{{libheader|Wren-complex}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./complex" for Complex
import "./fmt" for Fmt
 
class QuaterImaginary {
Line 3,944 ⟶ 4,134:
c2 = qi.toComplex
Fmt.print(fmt, imagOnly.call(c1), qi, imagOnly.call(c2))
}</langsyntaxhighlight>
 
{{out}}
2,122

edits