1

I have this code that gets X, Y, Z positions from each frame in ARKit.

let CamPosition = SCNVector3(transform.m41, transform.m42, transform.m43)

How would I round the numbers down because they output occasionally in scientific notation like this?

SCNVector3(x: 7.276927e-09, y: 2.4679738e-09, z: 3.395949e-10)

Instead of the desired output like this:

SCNVector3(x: 0.026048008, y: 0.0069037788, z: 0.010655182)

Any help is greatly appreciated!

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Winfield
  • 37
  • 4
  • Did [my answer](https://stackoverflow.com/a/67290112/14351818) not work? – aheze May 05 '21 at 02:03
  • 1
    your answer worked, but I'm still getting the scientific notation for extremely small numbers. I wanted to see if rounding would solve that. – Winfield May 05 '21 at 03:25

2 Answers2

0

Rounding to Meters

For that you can use three holy methods: round(_:), and ceil(_:), and floor(_:).

import SceneKit
import Foundation

let node = SCNNode()

node.position = SCNVector3(x: floor(12.856288),
                           y:  ceil(67.235459),
                           z: round(34.524305))


node.position.x    //  12
node.position.y    //  68   
node.position.z    //  35

Rounding XYZ values to integer, you make them to translate intermittently (discretely) in meters.


Rounding to Centimeters

Rounding XYZ values to 2 decimal places:

node.position = SCNVector3(x: round(12.856288 * 100) / 100.0,
                           y: round(67.235459 * 100) / 100.0,
                           z: round(34.524305 * 100) / 100.0)

node.position.x    //  12.86   (hundredths)
node.position.y    //  67.24
node.position.z    //  34.52
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
0

It seems to me like you're trying to fix that doesn't need to be fixed.

As you can see from this very comprehensive answer, computers are just not good at storing some decimal numbers, so what you're getting is the nearest neighbour that can be represented in binary.

Unless this is causing you functional issues, I would recommend ignoring it. If the problem is that your debug logs have numbers that are not easy to parse, use a number formatter.

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50
  • thank you for the answer! The issue is that I'm passing this data to a python script written by someone else. These numbers are giving them errors they say they can't fix on their side, which is why I'm trying to do it here. – Winfield May 05 '21 at 21:19