Page 1 of 1

Can't get StrToReal to Work

PostPosted: Mon Feb 07, 2011 9:35 pm
by SpiritualMadMan
I have tried everything I know to get this conversion to work.

Yes, the example: -36.5 works

But, as soon as I add an exponent it fails.

The Real Variable type is supposed to be good for +/- 1.7E308

All I need is engineering range fro Femto to Giga...

Using PTD 2.1 of Feb 6, 2011

An example of accepted ranges and formats for the StrToReal Function would be most appreciated.

Re: Can't get StrToReal to Work

PostPosted: Tue Feb 08, 2011 3:56 pm
by john
Hello-

This is a bug in Phantom and will be fixed in a maintenance release. In the mean time, you can use a user defined function as such:

Code: Select all
# Temporary fix to StrToReal bug
function real MyStrToReal(string s){
  string parts = split(s, "E");
  if(length(parts) == 1){
    return StrToReal(parts[0]);
  }else if(length(parts) == 2){
    real base = StrToReal(parts[0]);
    real exp = StrToReal(parts[1]);
    return base * 10.0^exp;
  }else{
    exception e;
    e.SetError("Invalid number: " + s);
    e.throw();
  }

  return 0.0;
}

real r;
disp(MyStrToReal("10.0"));
disp(MyStrToReal("-10.6"));
disp(MyStrToReal("1E+6"));
disp(MyStrToReal("1E-6"));
disp(MyStrToReal("12.421E60"));
disp(MyStrToReal("12.421E-60"));

r = MyStrToReal("12.421E-60")
disp(format("%E", r));


You can put the function in a .fun file and include it whenever you need to do a StrToReal (see 'include' in the Phantom help).

Sorry for any inconvenience this has caused, and I hope this helps!

-John

Re: Can't get StrToReal to Work

PostPosted: Tue Feb 08, 2011 5:38 pm
by SpiritualMadMan
Works as advertised!

Thanks John!