Calculating absolute value!

math
we_luv_maths
mathisfun

#1

Hello!! In this math tutorial,I will talk about absolute value. If there is already a topic like this,let me now! Now,back to topic.
what is absolute value?
it is calculating how far a number is from 0. Imagine a line with negative numbers on the left,positive on the right. -6’s absolute value would be 6,cause 6+ -6=0.
how do you calculate it?
As mentioned before,absolute value is the distance a number us from 0.
The easiest way is to:-
1 check if your number is positive or negative.
2 if it’s negative,you just have plus the negative number’s positive into itself.
It’s calculated like-
No= -10. Absolute value= -10+10=0.
If it’s positive,you do the opposite. You minus the positive number by itself.
It’s calculated like-
No= 10. Absolute value= 10-10=0.
Which means if it’s negative,you plus it by itself.
If it’s positive,you minus it by itself.
Easy,right?


#2

Can you tell how to calculate “INT” using Hopscotch?
A simple way that gets rid of the decimal places of a number?


#3

What’s ‘INT’? Please use the long name,if there is.


#4

I think INT is used in coding languages like Java. I am not sure this can used in hopscotch,because it is very different from coding languages like Java,C+,C++,etc. If you want to get rid of decimals,just write a simple whole number. You don’t need to put decimal points.


#5

Int(x) most likely means integer of the number “x”. You take x and make it an integer. An integer is a number with no decimals, only a whole number. If you have 12.5, 12 is the integer.

When showing an Angles degrees in Hopscotch and don’t want a long number with decimals, it’s good to use/simulate INT

I haven’t tried doing “INT” with Hopscotch but have used INT with Tynker and other coding programs.


#6

There’s no INT feature,though it is a good idea! Maybe ask THT about it?


#7

@Stradyvarious gave you a great explanation. Sometimes, the round() block could possibly work, but it does not work all the times. A more precise round()-function and int() and float() conversions like in Python and several other coding languages would be cool to have in Hopscotch!


#8

It sure would be!


#9

Maybe something like this:
Var 1= random(0,10)
Var 2= round(var 1)
If(Var 2>Var 1)[
Increase var 2 by -1
]
Var 2 would be Var 1’s integer, right?


#10

I don’t think it’s the best, bur

Check one if (Number>0)
(Round(Number-.5))
Else
(Round(Number+.5))
End

Might work.


Yes, but that wouldn’t work for negatives, and it’s not the easiest. It also takes longer to do. The same result could be achieved with

Var 2 = round(Var 1-.5)

#11

Hi @Stradyvarious @The_Vast_Void @Petrichor @William04GamerA
Truncation or Int( ) can be done with modulo. See below.

^ this is a round_down function. It’s the same as int( ) for positive numbers, but is different for negative numbers (which is what I think Nindroid was also explaining above)

Round Down
Round_Down(x) = Round(x-0.5)

Integer or Truncation
Int(x) = x - (x mod 1)

For positive numbers these give the same result, with the difference being Int(x) moves closer to 0, so for negative numbers Int(x) rounds up. Truncation is just keeping the integer portion of a number so is the same as an integer function or int(x).

Here’s an explanation / derivation for Int(x)

Just to restate, Int( ) is the same as truncation, which is rounding towards zero or just keeping the integer portion of a number.

Definition of modulo:
IMG_1008

where the bit with the "L"s is truncation

So we can solve for trunc(x) by letting y=1

x mod 1 = x - 1 * trunc(x/1)
x mod 1 = x - trunc(x)
trunc(x) = x - (x mod 1)

int(x) = trunc(x) = x - (x mod 1)



@Aariv
To keep this relevant to your OP… and just for fun

Here’s a definition for int(x) that uses absolute value or abs( )

Int(x) = round(abs(x)-0.5) * (x/abs(x))

Notice the first part is similar to the round_down function? And the multiplication by (x/abs(x)) either returns 1 or -1 so it makes the answer negative if x is negative. I wouldn’t actually use this one in practice as it’s more complicated than the formula using modulo. It was shown just for fun


#12

What does this mean?
X=number
Abs=absolute value
X/abs?
What does / stand for?


#13

That’s mathematic notation. Parentheses can either be used to define an order of operations or for a function (like a block in Hopscotch that takes an input variable)

So (x/abs(x)) is

The outer parentheses are for order of operation: Calculate x/abs(x) before the prior multiplication

abs( ) is the absolute value function

So it’s x divided by the absolute value of x. Let’s look at a couple examples.

If x = 2
x/abs(x) = 2/abs(2) = 2/2 = 1
where every positive value of x would equal 1

If x = -5
x/abs(x) = -5/abs(-5) = -5/5 = -1
where every negative value of x would equal -1


#16

Oh. Ok…


#17

this is so funny for some reason


#18

xse youre a freaking genius