Can someone explain to me what the floor and cealing functions are actually doing numerically?
Need guidance on math.floor
how does a floor function work? - Mathematics Stack Exchange
What exactly does Math.floor do?
Videos
When I truncate a number what my brain actually does is ignoring the fractional part of said number. But its not doing any real math.
I understand I can express a truncate function with conditional floor and cealing functions... but thats is not what I need.
I need someone to teach me how to arrive from a number to its integer using only mathematical operations and not logical functions.
I need to know...
Plz help me someone...
This is better suited for the programming forums but....
Your computer program is probably working with a binary representation of a number. To compute the floor function, the computer does exactly the same thing you do: e.g. if it holds a representation of the positive binary numeral
$$ 100110.01011101 $$
then it simply replaces every digit to the right of the point with a zero:
$$ 100110.00000000 $$
The processor your program runs on likely has assembly language instructions for performing this exact operation when a number is stored in a register in IEEE 754 standard format (which is almost always used to store floating-point numbers).
if you are asking about what the computer does it is like this: you have the variable $x$
$\lfloor x\rfloor=max\{m\in\mathbb{Z}~|~m\le x\}$
which means that out off all the integers that beneath $x$ take the largest.
now the computer doesn't has the function $\in$ or the group $\mathbb{Z}$ or any of those stuff so he do it differently, the computer save memory with $0$'s and $1$'s, bits, integer he saves with 32-bits(usually)
for understanding with 8-bits it looks like this:
$1111~1111$bits$=-127$
$1000~0000$bits$=1$
$0111~1111$bits$=0$
now for float he has a different method, 32-bit format looks like this:
$\underbrace{0}_{0=positive\\1=negative}\underbrace{00000000}_{the~exponent }~~\underbrace{00000000000000000000000}_{the~fraction~part}$
now how exactly this format works is not important now, but you can see from this format that if you have the float, for example, $0~~10000000~11000000000000000000000(=3.5)$ the computer can just ignore the last 22 bits and take only $0~~10000000~1$, the computer can extract all he needs from the first 10 bits if you do interested in how the float itself works:
the computer look at the first bit and put it in var name AXL(for this example) and do $AX=(-1)^{AXL}$ now he takes the last part and do $DX=1+\text{[the bit]}^\text{[the bit position]}+\text{[the bit]}^\text{[the bit position]}+...$
and the end result is:
$AX\times (DX\times 2^{\text{[the middle part value]}})$
now because that every part after the 10th bit is quarter or less you don't need them when you use floor
No, in fact this line of code outputs 0:
Math.floor(0.9);
Math.floor always rounds to the nearest whole number that is smaller than your input. You might confuse it with Math.round, that rounds to nearest whole number.
This is why this code always outputs 1 or 0, since input never gets to 2 or bigger:
Math.floor(Math.random()*2)
There's actually three different rounding functions in JavaScript: Math.floor, its opposite Math.ceil and the "usual" Math.round.
These work like this:
Math.floor(0.1); // Outputs 0
Math.ceil(0.1); // Outputs 1
Math.round(0.1); // Outputs 0
Math.floor(0.9); // Outputs 0
Math.ceil(0.9); // Outputs 1
Math.round(0.9); // Outputs 1
Math.floor(0.5); // Outputs 0
Math.ceil(0.5); // Outputs 1
Math.round(0.5); // Outputs 1
The floor() method rounds a number DOWNWARDS to the nearest integer, and returns the result.
If the passed argument is an integer, the value will not be rounded.



