Float point in vector (SOLVED)

I have the next code:

local vvvv1 = vmath.vector3(-300, -300, 0.1)
local vvvv2 = vmath.vector3(-300, -300, 0.2)
local vvvv3 = vmath.vector3(-300, -300, 0.3)
local vvvv4 = vmath.vector3(-300, -300, 0.4)
local vvvv5 = vmath.vector3(-300, -300, 0.5)
local ffff1 = 0.1
local ffff2 = 0.2
local ffff3 = 0.3
local ffff4 = 0.4
local ffff5 = 0.5
function init()
  print(vvvv1)
  print(vvvv2)
  print(vvvv3)
  print(vvvv4)
  print(vvvv5)
  print(ffff1)
  print(ffff2)
  print(ffff3)
  print(ffff4)
  print(ffff5)
end

And I receive next log:

DEBUG:SCRIPT: vmath.vector3(-300, -300, 0.10000000149012)
DEBUG:SCRIPT: vmath.vector3(-300, -300, 0.20000000298023)
DEBUG:SCRIPT: vmath.vector3(-300, -300, 0.30000001192093)
DEBUG:SCRIPT: vmath.vector3(-300, -300, 0.40000000596046)
DEBUG:SCRIPT: vmath.vector3(-300, -300, 0.5)
DEBUG:SCRIPT: 0.1
DEBUG:SCRIPT: 0.2
DEBUG:SCRIPT: 0.3
DEBUG:SCRIPT: 0.4
DEBUG:SCRIPT: 0.5

Whats wrong with z in vector? Đž_Đľ
And GO disappear when I this this vector as pos.

Regarding the z values, it’s not really a bug.
In Lua, numbers are represented by double precision floating point values (a.k.a. doubles)
Our vector3 is based on Sony’s vector lib, which uses single precision floating point values (a.k.a floats)
Doubles are 64bits, and floats are 32 bits.
Neither doubles or floats can represent all real numbers.

Since floats are much smaller (32 bits smaller!), they will lose precision much earlier than doubles.
Also, in some cases, you have to account for the actual print out of the number (which might not be the same)

Here is a C++ example, and there is a ton of material regarding floating point precision/accuracy and robustness etc out there.

result from c++ example:

0.1f : 0.10000000149011611938476562500000
0.1  : 0.10000000000000000555111512312578
0.1f + 0.2f = 0.30000001192092895507812500000000
0.1  + 0.2  = 0.30000000000000004440892098500626

As for why your object disappears, I think there’s something else going on.

EDIT: (Updated the c++ example and result, since the addition result was buggy) :slight_smile:
Also, I’d like to mention that this is why you never want to compare floating point numbers using the == operatior!

3 Likes

Thank you.
I supposed that problem is in different types or something like this (dependency of processor etc)

My next issue is to understand why my sprites hiden after I set Z. =(

What does your render script look like?

Yeah, I already thought about it, part with projection (current sprite predicate is “gui_particle_back_pred”):

  render.set_view(vmath.matrix4())
    render.set_projection(vmath.matrix4_orthographic(0, current_width, 0, current_height, -1, 1))

    render.enable_state(render.STATE_STENCIL_TEST)
    render.draw(self.gui_particle_back_pred)
    render.draw(self.gui_pred)
    render.draw(self.text_pred)
    render.draw(self.gui_particle_pred)
    render.draw(self.gui_spine)
    render.draw(self.top_gui)
    render.disable_state(render.STATE_STENCIL_TEST)

Also, could it be that the z values are accumulated? E.g. “go parent z” + “go z” + “sprite z” ? (I’ve forgot this a couple of times :slight_smile: )

:joy_cat::joy::joy_cat:
you right!
Thank you very much.
Sometimes really simple thing can waste whole day

1 Like

I didn’t pay attension that new object in collection, or added other collection set Z to 1 by default.