[Sidefx-houdini-list] ftoa problem and expression function
antoine at rhythm.com
Mon Oct 2 16:13:35 EDT 2006
luc froehlicher wrote:
> hi everyone.
> I'm trying to convert a binary number in string using ftoa
> something like :
> string myfunction(val)
> string sval=ftoa(val);
> return sval;
> I always get the exponential notation ie:
> echo `myfunction(00010001100)`
> is there a way to avoid exponential notation or a way to get around this.
I don't recall that there's a way to convert from binary to base 10
natively in hscript, so even if it understood the implicit conversion,
the resulting number wouldn't look remotely like your binary value!
There's a semi-hack that sort-of works in the UI, which is to declare
the result an integer instead of a float. But unfortunately, expression
functions can only return strings or floats.
But fundamentally, the problem is the storage of the floating point
value natively in the computer: 4 bytes, when what you're supplying is a
string that consumes 11 bytes. That's quite a bit of compression, so
1.00011e+7 is what you get!
I would store the number in base-10 via a perl script, e.g.
float bin2f(string binval)
string perlcmd = "perl -e 'printf(\"%d\", 0b";
perlcmd = perlcmd + binval + ")'";
work with it, and then when you need to spit it out again, apply a
float f2bin(float floatval)
string perlcmd = "perl -e 'printf(\"%b\", ";
perlcmd = perlcmd + floatval + ")'";
/ -> exread bin2f.func
/ -> echo `bin2f("1011")`
/ -> echo `f2bin(11)`
> the other thing is that I wrote several expressions functions , is there
> a way tohave them permanently available
> (ie without having to reload them everytime I launch houdini)
> Sidefx-houdini-list mailing list
> Sidefx-houdini-list at sidefx.com
More information about the Sidefx-houdini-list