Round off to third decimal places

   9111   8   1
User Avatar
Member
27 posts
Joined: Oct. 2018
Offline
Hi,

I'm trying round off to third decimal places.
The operation I did was 

float target *= 1000;
target = floor(target);
target /= 1000;

but, the result was not what I expected.
ex 1.33000162266


If you know solution,could you tell me?

Attachments:
Caluclation.JPG (86.9 KB)

User Avatar
Member
2127 posts
Joined: Sept. 2015
Offline
It's better if you post a hip file with a clear description of what you want to apply the result too.

As it is floating point values you are dealing with - it has ‘nothing’ to do with Houdini per se.

It is the nature of using computer hardware.

You may want to do some of your own research first like as one example here:

https://blog.penjee.com/what-are-floating-point-errors-answered/ [blog.penjee.com]

So posting your hip and what you want to accomplish, someone might be able to help you determine if what you have could be a problem or not.

If so, what you might be able to do.
User Avatar
Member
9 posts
Joined: Sept. 2017
Offline
I typed the following into a wrangle to test and it does the correct thing for me:

float target = 1.33440162266;
target *= 1000;
target += 0.5; //allow for rounding up or down
target = floor(target);
target /= 1000;

printf (“ %d\n”, target);
User Avatar
Member
682 posts
Joined: Sept. 2013
Online
Hi kazurati,

this works for me on all sorts of random values:

f@num_rand = rand(@Frame);
f@num_thsd = floor(@num_rand * 1000);
f@num_clip = @num_thsd / 1000.0;

eg:

0.380973 -> 380.0 -> 0.38
https://procegen.konstantinmagnus.de/ [procegen.konstantinmagnus.de]
User Avatar
Member
27 posts
Joined: Oct. 2018
Offline
Hi,Everyone.

Thank you for comment.

I cannot explain my issue very well.
so, I attached my hip file.

As you can see, a target value is not rounded …

Anyone please help me.

Attachments:
Round.JPG (206.5 KB)
Cant_floor.hipnc (70.3 KB)

User Avatar
Member
8785 posts
Joined: July 2007
Offline
as mentioned previously, it's has to do with floating point precision
even if you do: f@target = 0.998;
you will see that the Font SOP will still print something else, like: 0.99800002574920654

it's because it can't represent 0.998 precisely as a floating point number

so you'll need to format your floating point number into a string using sprintf() for example

Attachments:
Cant_floor_fix.hipnc (71.1 KB)

Tomas Slancik
FX Supervisor
Method Studios, NY
User Avatar
Member
5 posts
Joined: Sept. 2018
Offline
tamte
as mentioned previously, it's has to do with floating point precision
even if you do: f@target = 0.998;
you will see that the Font SOP will still print something else, like: 0.99800002574920654

it's because it can't represent 0.998 precisely as a floating point number

so you'll need to format your floating point number into a string using sprintf() for example


From Tomas attachment there's some function & argument am not familiar with, so incase someone is struggling as I was

When you use
sprint("%f", floatValue);
will convert float value into string
This work whether you type hard core floats or you're using chf values.

When you want to trim decimal places like 1 or 2 or 3 decimal place you will have to write like

sprint("%.2f", floatValue);
This means convert trim to 2 decimal places, i.e 0.256234 will become 0.256

Hopefully this will be helpful as well.
Edited by mujuningaiza - Dec. 13, 2022 11:08:03
User Avatar
Member
8041 posts
Joined: Sept. 2011
Offline
Assuming adding a layer of vex to convert from float to string is a pain, the ftrim() expression can be used to pretty print floats as strings.

ftrim(detail(-1, "test", 0))

Attachments:
ftrim.hip (117.9 KB)

User Avatar
Member
5 posts
Joined: Sept. 2018
Offline
jsmack
Assuming adding a layer of vex to convert from float to string is a pain, the ftrim() expression can be used to pretty print floats as strings.

ftrim(detail(-1, "test", 0))

Thanks @jsmack for showing me how to use ftrim expression this is helpful, although I can't trim to how many decimals I want. Although with
sprint("%.3f", 0.7838759687)
I can Trim to 3 decimal place.
I believe adding
sprintf
under the belt is not bad idea.
Edited by mujuningaiza - Dec. 14, 2022 05:10:02
  • Quick Links