Talk:Roots of unity: Difference between revisions

Writing a complex number as -0.5000+0.8660i
(Oh ok...)
(Writing a complex number as -0.5000+0.8660i)
Line 56:
What do you think? [[User:Thomas Mertes|Thomas Mertes]] 09:35, 21 January 2008 (MST)
:Whatever is easier is fine as long as other people can figure it out. Maybe the output should explained a bit in the example. --[[User:Mwn3d|Mwn3d]] 09:52, 21 January 2008 (MST)
::I have reconsidered the issue aggain. The output format of a complex number should be self-explanatory. Your question showed me that my solution was counter intuitive. Now I think that writing a complex number as -0.5000+0.8660i would be the least confusing solution. This is also the mathematical notation and the format used by perl and similar to the format used in some other languages.
The Seed7 'parse' function to convert a string in the format -0.5000+0.8660i to a complex would be:
<pre>
(**
* Return the conversion of a string to a complex.
*)
const func complex: (attr complex) parse (in string: stri) is func
result
var complex: result is complex.value;
local
var integer: pos is 0;
var integer: pos2 is 0;
begin
pos := rpos(stri, '+');
pos2 := rpos(stri, '-');
if pos2 > pos then
pos := pos2;
end if;
if pos <> 0 and stri[length(stri)] = 'i' then
result.re := float parse (stri[.. pred(pos)]);
result.im := float parse (stri[pos .. pred(length(stri))]);
else
raise RANGE_ERROR;
end if;
end func;
</pre>
This function is less complicated than I thought. The sign of the imaginary part is used as separator between real and imaginary part.
I will include this change in the next release of Seed7. After the release I will change the output of the "Roots of unity" example. [[User:Thomas Mertes|Thomas Mertes]] 06:36, 27 January 2008 (MST)