2

So I assigned a floatproperty inside my script. As I moved along I noticed differences that was unexplainable. SO I started to check the properties and how the system take care of them.

Result is

  lenght1: FloatProperty(
        name="Lenght1", unit="LENGTH", default=40.0 / 1000,
    )

Prints as

enter image description here

As you can see, the property that is 40 became 39.9xx?

Any idea if this is a bug?

Since I do a lot of calculations this becomes an issue and some results get off by more then 1 which is a no no

cecevo
  • 170
  • 1
  • 8
  • 1
    A solution for this is to actually store strings instead of float values, and parse them every time you want to do calculations. (eg length1: bpy.props.StringProperty() and then length1 = "40.0") This is a very well know problem with computer calculations and optimization. – Gorgious Nov 30 '21 at 08:18
  • @Gorgious Thanks for pointing this out. This indeed makes the precision a lot better. – cecevo Nov 30 '21 at 18:14

1 Answers1

3

It is not a bug. Floating point numbers are only accurate to about 8 decimals in Blender because it uses CPU single precision internally. Depending on what you mean by a lot you might want to investigate numpy which uses double precision internally.

Marty Fouts
  • 33,070
  • 10
  • 35
  • 79
  • Thanks for the reply. May I ask, As you see I made the property 40.0 and not 39.9 So what makes it change from my initial 40 to 39.9? – cecevo Nov 29 '21 at 20:49
  • 3
    It's not 39.9 but 39.999999... which in 8 bit precision is equal to 40 – Gorgious Nov 29 '21 at 20:58
  • Sorry I didn't reply sooner. I've been traveling. @Gorgious is right. It's a quirk of how floating point numbers are generated that they often end up being represented with repeating-last-digit representations. – Marty Fouts Dec 01 '21 at 01:28