• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Trunc(Sqrt(n))

Status
Not open for further replies.

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,201
Programming languages generally offer you a variety of conversion functions for reals.

truncation generally degrades the precision of a real by reducing it to a certain precision. It does this by physically removing bits from the result (such as by using a mask). If truncating to an integer it will generally perform identically to "floor".

Floor will round down a real to an integer. The fractional component is discarded completely, even if it was almost a whole unit.

Ceiling will round up a real to an integer. As long as there is any fractional component it will always add 1 to the result.

Round (or any similar name) will round to the nearest integer. Generally this is done with the fractional components being >=1/2 being treated as +1 otherwise discarded. It is important to read the documentation for such rounding as they might not always behave like this.

Generally you want to avoid using integers for things such as square root of small numbers as the precision is meaningless (square root of 4-8 inclusive will return the same integer).
 

Cokemonkey11

Spell Reviewer
Level 30
Joined
May 9, 2006
Messages
3,537
Level 23
Joined
Apr 16, 2012
Messages
4,041
Basically, you try to call Sqrt with argument n, lets say Sqrt(5). This will produce the square root of the passed argument, like 5, which is 2,2360679774997896964091736687313 according to windows calculator. Then this number is taken, and is passed to Truncate, which will take the number and cut it at the decimal point. In this case, this should be 2 and this is returned.
 

Cokemonkey11

Spell Reviewer
Level 30
Joined
May 9, 2006
Messages
3,537
Yeah, basically there's a composition of functions, just like in mathematics and in any other programming language. The innermost function takes precedence; the sqrt is evaluated first, followed by the truncation.

6l1Wyky.gif


Someone please close this thread before I blow my head off
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,201
No, that is incorrect. Almost globally, int(-3.5) != floor(-3.5)

Trunc() drops the fractional component.

The floor example I gave is true for unsigned integers and certainly for the example given seeing how square root function in C/C++ always returns a positive real number.

truncation generally degrades the precision of a real by reducing it to a certain precision
Is also true says Wikipedia .

You can also truncate data such as strings and files which will result in physically discarding parts of it to a certain length (no rounding behaviour). This is where the other part of my definition came from although you are correct it is not correct for numbers as rounding should always be towards 0 and data truncation on a fixed point number would result in the floor function.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,201
Who in their right mind would supply a uint ?
Positive only fixed point mathematics? I am not referring to a specific implementation but generic meanings. For example truncate might not necessarily have to round to whole numbers, it is perfectly valid to truncate one fractional value to a less precise fractional value. In your case you are using truncate to convert a floating point number into an integer so you are basically truncating to 0 decimal places however there could exist an implementation of truncate to convert a more precise floating point number to a less precise floating point number or from a more precise fixed point number to a less precise fixed point number.

You seem to be nit-picking every tiny little point I make which is not very nice...
 

Cokemonkey11

Spell Reviewer
Level 30
Joined
May 9, 2006
Messages
3,537
Positive only fixed point mathematics? I am not referring to a specific implementation but generic meanings. For example truncate might not necessarily have to round to whole numbers, it is perfectly valid to truncate one fractional value to a less precise fractional value. In your case you are using truncate to convert a floating point number into an integer so you are basically truncating to 0 decimal places however there could exist an implementation of truncate to convert a more precise floating point number to a less precise floating point number or from a more precise fixed point number to a less precise fixed point number.

You seem to be nit-picking every tiny little point I make which is not very nice...

You should always specify in cases where ambiguity is resolved. A beginner asking "what is truncate?" should never be told "essentially the same thing as floor function".

If you can think of a standard library whose single-argument truncate function does anything other than remove a fractional component, please let me know.

If you want to be so generic, why not just supply the definition of the word truncate?

shorten (something) by cutting off the top or the end.

there - now Daffa the Mage should definitely understand!
 
Status
Not open for further replies.
Top