# Problem with float multiplication?

Noticed this when multiplying two floats that I always get the wrong outcome.
I tried multiplying 1f with 500f, the result was 0.1
Then I tried 1f with 123f, result was 0.1
And then 2f with 123f, result was 0.2
I have absolutely no clue about what’s going on

Can you show us the code you’re using?

This is where I do the multiplication

``````public float movementSpeed = 123;
public Rigidbody rb;
public void Movement()
{
float xDir = Input.GetAxis("Horizontal");
float zDir = Input.GetAxis("Vertical");
Vector3 moveDir = new Vector3(xDir, 0.0f, zDir);
rb.velocity = moveDir * Time.fixedDeltaTime * movementSpeed;
}
``````

The resulting vector is so ridiculusly small that there is no visible movement

Time delta has shrunken the value so that it is a fraction of 1.

Well it shrinks it even further, but when I took xDir (which was 1 or less) and multiplied with movementSpeed, the result was 0.10 or lower.
Thats obviously not the correct outcome

What kind of number are you after ?

1 ?

If the number is less than the first whole number in mathematics then you are effectively asking for a fraction of your number.

Multiplying by 0.5 half of the first whole; 1, will result in a division.

The rules of mathematics consider division to be the opposite of multiplication.

Logic may depict that I am multiplying a value so therefore I would expect a higher value than I had, but as the concept of having less than the first whole is applied to multiplication you would not otherwise recieve MyValue increased by 0.1 but you would see a division of that value. At 10% it’s original size.

When setting a rigidbodies velocity directly, do not include any delta time.

What I am talking about is the fact that during runtime, when calculating 1.0f * 123f, the result was 0.1 when it should have been 123f

Why is that?

Did you to force the debug console to confirm that moveDir * fixedTime = 1 ? Before multiplying that result * speed?

No I multiplied 1.0 by 123.0 and then Debug.Log-ed the result which was 0.1

Fascinating o.O

I would love to see the screen shot

The physics system does that for you. It is the velocity not how much it moves per update, but per second. Think of it as units per second.

1 Like

OMG, I hate debugging!
Just realized while making a screenshot that movementSpeed was being manually assigned a new value in another place. Had no idea

But thanks anyways!

Ah ok, thanks! Didn’t realize

no problem

1 Like

For future reference, here you go:

Remember the six stages of debugging:

1. That can’t happen.
2. That doesn’t happen on my machine.
3. That shouldn’t happen.
4. Why does that happen?
5. Oh, I see.
6. How did that ever work?

Personally, I absolutely LOVE debugging. Debugging makes me realize how faint a grasp we humans have on reality. We really barely understand wtf is ever going on 99% of the time. Debugging is an excellent and humbling lesson and can be used to guide you to be more critical of things you hear or think are true.

2 Likes

You speak the truth