Search - User list
Full Version: truncating floats and weird trig results
Root » Technical Discussion » truncating floats and weird trig results
Henster
Hi,

I'm doing something like this:

atan($X/$Y)

and I'm getting weird results. I've found an example of what is causing it. A number like this crops up 1.61499e-00 (from the geometry spreadsheet). If I then rig this same calculation but exchange this number for a truncated version 1.61499 then it works.

I cant find a function that truncatesto a few floating points???

Thanks for any replies
Antoine Durr
Henster
Hi,

I'm doing something like this:

atan($X/$Y)

and I'm getting weird results. I've found an example of what is causing it. A number like this crops up 1.61499e-00 (from the geometry spreadsheet). If I then rig this same calculation but exchange this number for a truncated version 1.61499 then it works.

I cant find a function that truncatesto a few floating points???

Thanks for any replies

First of all, the geo spreadsheet probably truncates what you see, so you probably shouldn't use it to really gauge this. Instead, write out a .geo file and look at the values in there.

That said, the Houdini math libraries have (since the PRISMS) days had problems with really, really small numbers. I would add in (via a point SOP) something like if(abs($TX)<.00001,0, $TX), i.e. knock all near-zero values down to zero.

– Antoine
Henster
These values are in pops so I dont think they'll show up in a geo file.

Using the if((abs($TX)<0.0001,0,$TX) thing only truncates numbers that are close to zero. It looks like the problems are coming just when there are loads of decimal places.
deecue
if you're wanting to just truncate the number of decimal places, here's an odd way of doing it:

rint(val*10^n)*10^(-n)

where n is the number of decimal places you want.


so say you wanted to truncate a value to 3 decimal places..

Value: 3.4832165132165132158

rint(3.4832165132165132158*1000)*.001
= rint(3483.2165132165132158)*.001
= 3483*.001
= 3.483

put in your expression for val and it should work. can make some interesting step curves as well depending on the number of decimal places you allow for.

hth,
dave
Antoine Durr
Henster
These values are in pops so I dont think they'll show up in a geo file.

If you set the cook/display flag to the node where you found your suspected problem, write out the .geo from the popnet SOP.

– Antoine
asnowcappedromance
deecue
if you're wanting to just truncate the number of decimal places, here's an odd way of doing it:

rint(val*10^n)*10^(-n)

where n is the number of decimal places you want.


so say you wanted to truncate a value to 3 decimal places..

Value: 3.4832165132165132158

rint(3.4832165132165132158*1000)*.001
= rint(3483.2165132165132158)*.001
= 3483*.001
= 3.483

put in your expression for val and it should work. can make some interesting step curves as well depending on the number of decimal places you allow for.

hth,
dave

hi,

very interesting topic,

but is there really no other way of truncating decimal places via expressions???

regards,

Manu
Antoine Durr
asnowcappedromance
very interesting topic,

but is there really no other way of truncating decimal places via expressions???

Not as far as I know. The truncation of decimal places is not a feature of floating point values as represented internally in the computer. Internally, the value 1 is actually 1.00000 (with however many decimal points allowed by the particular float/double representation).

The age-old approach is to truncate on printing. The C stdio library has a bunch of options to it's printf() function. I don't have H in front of me, so I don't know to what extent the %f or %g options are implemented. In C, you can print %.2f, which will show the value 1 as 1.00, or %g which will show 1.23000 automatically as 1.23.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB