Can't get StrToReal to Work

General discussion about the Phantom Automation Language and related tools.

Can't get StrToReal to Work

Postby SpiritualMadMan on Mon Feb 07, 2011 9:35 pm

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.
SpiritualMadMan
 
Posts: 20
Joined: Mon Dec 06, 2010 4:02 pm

Re: Can't get StrToReal to Work

Postby john on Tue Feb 08, 2011 3:56 pm

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
john
 
Posts: 27
Joined: Wed Apr 22, 2009 12:32 pm

Re: Can't get StrToReal to Work

Postby SpiritualMadMan on Tue Feb 08, 2011 5:38 pm

Works as advertised!

Thanks John!
SpiritualMadMan
 
Posts: 20
Joined: Mon Dec 06, 2010 4:02 pm


Return to Phantom

Who is online

Users browsing this forum: No registered users and 10 guests

cron