0

If we pass some float value to the argument of Vector3Int, how does it handle it?

m_target = new Vector3Int(target.x-speedx/4,target.y,target.z-speedz/4);

so if speedx/4 will be float then how will it be converted to int

Vector3Int(124.66,0,0) would be considered as (124,0,0) or (125,0,0)

Prateek Pandey
  • 1,077
  • 2
  • 8
  • 14
  • What types are `target.x` and `speedx`? – Lasse V. Karlsen Jul 02 '19 at 07:53
  • @LasseVågsætherKarlsen they both are int – Prateek Pandey Jul 02 '19 at 09:51
  • Then you don't have a float value at all, `int/int` gives another `int`, for instance if `speedx = 15`, `speed / 4` will give `3`, not `3.33333.....`. – Lasse V. Karlsen Jul 02 '19 at 09:55
  • @LasseVågsætherKarlsen so basically the automatic type casting is done on speedx/4 not (target.x - speedx/4)? – Prateek Pandey Jul 02 '19 at 09:57
  • No, there is no type casting, `/` between ints produce another int, there's no casting involved. This is simply integer division. Basically you have `int + int / int`, all of this will just produce another int, float has never been in the picture. – Lasse V. Karlsen Jul 02 '19 at 10:49
  • However, your question, "If we pass some float value to the argument of Vector3Int, how does it handle it?", you will have to look at the answers here to get your answer for that, but your code doesn't use floats, so the answer is largely irrelevant. Whether you *should* be using floats or not is a completely different question though. – Lasse V. Karlsen Jul 02 '19 at 10:51
  • I'm voting to close this question as off-topic because this is a "why don't you try it and find out" kind of question. – Draco18s no longer trusts SE Jul 02 '19 at 13:19

2 Answers2

1

NOTE ADDED AFTER THE COMMENTS TO THE QUESTION

In the code you posted you're doing integer division, that sends has int as result (see also this answer).

From what I read in the comments. target.x, target.y, speedx and speedz (and also the constant 4) are all integers.

See also this example:

int x = 13 / 4; //this returns int 3

So you need no casting there.


Answer to the question float to Vector3Int

Float does not convert to int implicitly, so your code won't compile. You need a cast for the conversion.

Consider also that the conversion truncates the float.

float x = 124.345f;  
int y = (int) x;   //x will become 124.

In your specific case you can convert it like this.

m_target = new Vector3Int((int) target.x-speedx/4, (int)target.y, (int)target.z-speedz/4);

And the result will be (124.0, 0.0, 0.0).

If you want to achieve other results you may check these methods.


FLOAT TO INT CONVERSIONS

Mathf.Floor and Math.FloorToInt

The end result will be just as the base cast. The float will be truncated to the lower int.

float x 124.345;
int y = (int) Mathf.Floor(x); //y will become 124
//or
int z = Mathf.FloorToInt(x);  //z will become 124

CONVERSION RESULTS
float 124.345f → converts to int 124
float 124.789f → converts to int 124


Mathf.Ceil and Mathf.CeilToInt

The float will be truncated to the higher int.

float x 124.345;
int y = (int) Mathf.Ceil(x); //y will become 125
//or
int z = Mathf.CeilToInt(x); //z will become 125

CONVERSION RESULTS
float 124.345f → converts to int 125
float 124.789f → converts to int 125


Mathf.Round and Mathf.RoundToInt

The float will be rounded to the closer int.

float x 124.345;
int y = (int) Mathf.Round(x); //y will become 124
//or
int z = Mathf.RoundToInt(x);  //z will become 124

CONVERSION RESULTS DIFFERS
float 124.345f → converts to int 124
float 124.789f → converts to int 125

Jack Mariani
  • 2,270
  • 1
  • 15
  • 30
  • 1
    All of your links still return `float` ;) Look at [my answer](https://stackoverflow.com/a/56847648/7111561) you have to use the `int` versions. – derHugo Jul 02 '19 at 07:51
  • Yes, sorry, I was already editing the answer. Thanks for the heads up. – Jack Mariani Jul 02 '19 at 07:55
  • Note that it still isn't correct: `Math` doesn't have a method like `RoundToInt` .. it is Unity's library `Mathf` (note the `f`) – derHugo Jul 02 '19 at 07:57
  • I get that we need to typecast it to int, but my problem is that i am converting some c# code in Erlang, so that c# code i cannot change, i want to know how i should do it in erlang (ceil or floor or roundof considering that erlnag dont have this Vector3Int), so how should i handle if in c# we get float value as argument to that – Prateek Pandey Jul 02 '19 at 07:58
  • derHugo, exactly. @PrateekPandey. What exactly you need to pass to Erlang? You might just do the casting in C# and pass the result to Erlang. – Jack Mariani Jul 02 '19 at 08:02
  • @JackMariani the code is working in C#, i am writing its backend in erlang, so for syncing need to write exactly the same, the problem is say Client says the Target is (127,0,0) but on server side its (126,0,0). – Prateek Pandey Jul 02 '19 at 09:56
  • @PrateekPandey in my networking opinion you should not do the calculation both on server and on client. You do the calculation on server and send the result on client, or you predict the position on client and "legalize" it on server (check that the position is not changed too much from the last network tick). Anyway I think I've not the space in the comment to answer Erlang conversion, and if I add the solution in the answer I'm going totally off topic. I would suggest to solve this and open a new question. – Jack Mariani Jul 02 '19 at 10:06
  • Also in [this answer](https://stackoverflow.com/questions/4006201/truncate-a-float-in-erlang) you may see how to Floor the erlang float. [This](http://site4fast.blogspot.com/2011/10/erlang-float-to-integer.html) might be useful too. – Jack Mariani Jul 02 '19 at 10:14
1

It won't compile.

You have multiple options for converting the values to int depending on your needs.

But first make especially sure that either speedx is of type float or use 4f otherwise you you might already doing an int division there using incorrect results.


Simple Typecast | FloorToInt

new Vector3Int((int)(target.x - speedx/4f), (int)(target.y), (int)(target.z-speedz/4f));

It simply "cuts of" anything behind the comma. It therefore behaves kind of equaliy to Mathf.FloorToInt

(124.6f, 0, 0) → (124, 0, 0)
(124.4f, 0, 0) → (124, 0, 0)


RoundToInt

you can use Mathf.RoundToInt instead

new Vector3Int(Mathf.RoundToInt(target.x-speedx/4f), Mathf.RoundToInt(target.y), Mathf.RoundToInt(target.z-speedz/4f)); 

for correctly up and down rounding the value to a valid int.

(124.6f, 0, 0) → (125, 0, 0)
(124.4f, 0, 0) → (124, 0, 0)


CeilToInt

If you want it to be always abrounded you can also use Mathf.CeilToInt

new Vector3Int(Mathf.CeilToInt(target.x-speedx/4f), Mathf.CeilToInt(target.y), Mathf.CeilToInt(target.z-speedz/4f)); 

(124.6f, 0, 0) → (125, 0, 0)
(124.4f, 0, 0) → (125, 0, 0)

derHugo
  • 83,094
  • 9
  • 75
  • 115