mardi 27 octobre 2020

Why does Math.random() round to incorrect values

Suppose I write

System.out.println (Math.random()*5);

then one would obtain and output of x in [0, 4.9...]. But upon casting it as an integer, the result I continuously see (in my course) is [0, 4]. My question is how we are defining the rounding function; I am familiar with the floor function, and the floor of 4.9... is precisely 5 due to there existing no epsilon larger than zero satisfying the output x existing in some epsilon neighborhood; i.e., the equality 4.9... = 5 suffices and because the floor of an integer is that integer, the result would be 5.

Where am I going wrong here?




Aucun commentaire:

Enregistrer un commentaire