
-----------------------------------
xX_MayDay_Xx
Mon Mar 20, 2006 8:25 am

Math.abs
-----------------------------------
ok....i know that Math.abs gives the absolute value of a number..ex(the absolute value of -12...is..12) put i need to know how to do this without using the Math.abs application....i think it has something to do with multipling the number by it's self then dividing by 2?

-----------------------------------
Andy
Mon Mar 20, 2006 8:26 am


-----------------------------------
how about give us the definition of absolute value, once you understand what it means, this question becomes intuitive

-----------------------------------
xX_MayDay_Xx
Mon Mar 20, 2006 8:37 am


-----------------------------------
yeah.....i was an idiot......basic rule of math....anything *1 equals its self...that combined with the rules of integers -*+=-....or -*-=+...thats all  had to do....i was an idiot...i will no freely except harassment for the uber n()()b...
public class AbsoluteValue
{
	public static void main(String[]args)
	{
		int num;
		System.out.println("please enter a number");
		num=In.getInt ();
		if (num>=0)
		{
		System.out.println("the absolute  value of that number is..."
		                   + num);
	    }
	    else
	    {
			System.out.println("the absolute value of "+num+" is..."
			                  +num*-1);
						  }
	}
}


-----------------------------------
Andy
Mon Mar 20, 2006 8:40 am


-----------------------------------
there ya go  :)

-----------------------------------
xX_MayDay_Xx
Tue Mar 21, 2006 8:32 am


-----------------------------------
ummm....whats a switch application?

-----------------------------------
Krabjuice
Tue Mar 21, 2006 2:43 pm


-----------------------------------
Something that uses the switch statement, i'd assume.  Switch is a "in the case of" type of control sequence.  People generally use if's, but switch has it's uses.

-----------------------------------
Dan
Tue Mar 21, 2006 3:13 pm


-----------------------------------
I think the most effsent way of doing this whould be to set the singed bit to 0 or what ever means postive on your cpu.

Tho this whould probly be a bit complicated for most peoleop and is not real high level progaming but rather low level asm type stuff. I guse this is what Math.abs is for =p.

-----------------------------------
md
Tue Mar 21, 2006 4:10 pm


-----------------------------------
I dunno dan; getting a single bit isn't easy; I think a strait compare might be faster

n is $2
$3 is temporary storage

compare:

bgtz $2, ret
addi $3, $0, -1
mult $2, $3
mflo $2
ret:


bit-wise

addi $3, $3, 0
sra $3, $3, 31
bne $3, $0, ret
addi $3, $0, -1
mult $2, $3
mflo $2
ret:


-----------------------------------
Dan
Tue Mar 21, 2006 6:40 pm


-----------------------------------
I am prity shure that in asm at least seting one byte to 0 with a mov instruction is faster.

-----------------------------------
md
Tue Mar 21, 2006 6:47 pm


-----------------------------------
Wha? I'm using words... and I don't set any to zero.. 

setting the sign bit to 0 instead of 1 would not change the sign of the number at all, it would radically change the number though. 

1110 + 0010 = 0000. 0010 is the positive number, 1110 is it's negative. 0110 + 0010 = 1000, clearly not zero ;)

-----------------------------------
rizzix
Wed Mar 22, 2006 3:17 pm


-----------------------------------
1101 & 0111 = 0101
^-- sign bit


I believe that's what Dan was aiming for.

-----------------------------------
md
Wed Mar 22, 2006 4:21 pm


-----------------------------------
yeah, but just checking it is still slow. It's best case two instructions to compare just hte bit, whereas a simple less then is only one.

-----------------------------------
rizzix
Wed Mar 22, 2006 5:39 pm


-----------------------------------
1101 & 0111 = 0101
^-- sign bit / resut is a positive number: 0xxx

0110 & 0111 = 0110
^-- sign bit / result is a positive number 0xxx

:) It is fast. But as we discussed in #compsci.ca it isin't actually the case. Two's complement is a whole lot trickier than just fixing one bit.
