[Sidefx-houdini-list] ftoa problem and expression function

Antoine Durr 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)`
> 1.00011e+007
> 
> 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 + ")'";

   return(system(perlcmd));
}

work with it, and then when you need to spit it out again, apply a
reverse function:

float f2bin(float floatval)
{
   string perlcmd = "perl -e 'printf(\"%b\", ";
   perlcmd = perlcmd + floatval + ")'";

   return(system(perlcmd));
}

/ -> exread bin2f.func
/ -> echo `bin2f("1011")`
11
/ -> echo `f2bin(11)`
1011


-- Antoine


> 
> 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)
> 
> thanks
> luc
> _______________________________________________
> Sidefx-houdini-list mailing list
> Sidefx-houdini-list at sidefx.com
> https://lists.sidefx.com:443/mailman/listinfo/sidefx-houdini-list





More information about the Sidefx-houdini-list mailing list