commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Luc Maisonobe <Luc.Maison...@free.fr>
Subject Re: [math] ComplexFormat parse for inverted complex number
Date Mon, 11 Oct 2010 21:23:38 GMT
Le 11/10/2010 22:38, Grzegorz Szpetkowski a écrit :
> I have a question how can I (easily) parse String like that:
> 
> String sZ = "20i + 5.4";
> 
> On page http://commons.apache.org/math/userguide/complex.html I found example:
> 
> ComplexFormat cf = new ComplexFormat();
> Complex c = cf.parse("1.110 + 2.222i");
> 
> It's ok, but if I have inverted form like "20i + 5.4" then there is
> exception: "org.apache.commons.math.MathRuntimeException$9:
> unparseable complex number: "20i + 5.4".

I don't think it's possible for now.
One way to change that would be to add a flag "imaginaryFirst" in
ComplexFormat and change the parsing logic in the public Complex
parse(String source, ParsePosition pos) method according to this flag.

Are your numbers always in inverted order or do you have both orders ?
In the first case an appropriate semantic for the flag would be to
always use the order specified (in both parse and format). In the second
case, the semantic would rather be to accept both inverted numbers and
classical numbers on parse, but to always put real part first on format.

What do you think ?

Luc

> 
> Pseudo-algorithm for proposed solution:
> 1) Parse first number
> 2) IF first number has imaginary character THEN remember that second
> number should be real number (without imaginary character)
> 3) Parse second number
> 4) IF second number has imaginary character and first had it too (look
> at 2)), THEN throw exception
> Thanks ! Greg
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message