Arithmetic/Complex: Difference between revisions

Content deleted Content added
Blek (talk | contribs)
Franck (talk | contribs)
Line 2,793: Line 2,793:
=={{header|Oforth}}==
=={{header|Oforth}}==


<lang Oforth>100 Number Class newPriority: Complex(re, im)
<lang Oforth>Number 100 Class newPriority: Complex(re, im)
Complex method: re { @re }
Complex method: re @re ;
Complex method: im { @im }
Complex method: im @im ;
Complex method: initialize { := re := im }
Complex method: initialize := im := re ;
Complex method: << { '(' <<c @re << ',' <<c @im << ')' <<c }
Complex method: << '(' <<c @re << ',' <<c @im << ')' <<c ;
Integer method: asComplex { Complex new(self, 0) }
Integer method: asComplex self 0 Complex new ;
Float method: asComplex { Complex new(self, 0) }
Float method: asComplex self 0 Complex new ;


Complex new(0, 1) Constant new: I
Complex new(0, 1) Constant new: I


Complex method: ==(c) { c re @re == c im @im == and }
Complex method: ==(c) c re @re == c im @im == and ;
Complex method: norm { @re sq @im sq + sqrt }
Complex method: norm @re sq @im sq + sqrt ;
Complex method: conj { Complex new(@re, @im neg) }
Complex method: conj Complex new(@re, @im neg) ;
Complex method: +(c) { Complex new(c re @re +, c im @im +) }
Complex method: +(c) Complex new(c re @re +, c im @im +) ;
Complex method: -(c) { Complex new(c re @re -, c im @im -) }
Complex method: -(c) Complex new(c re @re -, c im @im -) ;
Complex method: *(c) { Complex new(c re @re * c im @im * -, c re @im * @re c im * + ) }
Complex method: *(c) Complex new(c re @re * c im @im * -, c re @im * @re c im * + ) ;
Complex method: inv
Complex method: inv
{
| n |
| n |
@re sq @im sq + asFloat ->n
@re sq @im sq + asFloat ->n
Complex new(@re n /, @im neg n / )
Complex new(@re n /, @im neg n / ) ;

}
Complex method: /(c) { c self inv * }</lang>
Complex method: /(c) c self inv * ;</lang>


Usage :
Usage :


<lang Oforth>2 3.2 I * + println
<lang Oforth>2 3.2 I * + .cr
Complex new(2, 3) 1.2 + println
Complex new(2, 3) 1.2 + .cr
Complex new(2, 3) 1.2 * println
Complex new(2, 3) 1.2 * .cr
2 Complex new(2, 3) / println</lang>
2 Complex new(2, 3) / .cr</lang>


{{out}}
{{out}}