Name: Anonymous 2006-10-30 4:08
First post, curious as to what kind of response I'll get...
Basicaly, just wondering if anyone'd be willing to take a quick peek at this, and clue me in on a fix for the exception PCspim's throwing at me, or offer any usefull comments.
I realy can't see why that function is any different than what's in the higher-level language version. I'm still fairly new to the Computer Science scene (as you'll see below) so hopfully you'll be gentle...:
##################################################
#
#
.text
.globl main
main:
li $v0, 6 #
syscall # load float
mov.s $f12, $f0 #
jal sqroot # test function
mov.s $f12, $f0 #
li $v0, 3 #
syscall # print results
li $v0, 10 #
syscall # exit program
# b in $f12
# ret in $f0 (x)
sqroot:
li $t0, 0 #
mtc1 $t0, $f2 #
cvt.s.w $f16, $f2 # cast floating point zero
mov.s $f4, $f16 # old guess
mov.s $f0, $f16 # x
mov.s $f8, $f16 # difference
c.eq.s $f12, $f16 # if (b == 0)
bc1t return # return 0
li $t0, 2 #
mtc1 $t0, $f2 #
cvt.s.w $f6, $f2 # cast floating point 2
l.s $f18, precision # Guessing precision
loop:
mov.s $f4, $f0 # save old guess
div.s $f10, $f12, $f0 # temp = (b / x)
add.s $f10, $f10, $f0 # temp = (b / x) + x
div.s $f0, $f10, $f6 # x = ((b / x) + x) / 2
sub.s $f10, $f4, $f0 # temp = old - x
abs.s $f8, $f10 # difference = |temp|
c.le.s $f8, $f18 #
bc1f loop #
return:
jr $ra
#
#
#########################################################
////////////////////////////////////////////////////////
/// What everything -should- be working like, in Java.
/**
* @author *Omitted*
* Driver program to test quadratic and square root functions.
*/
import java.util.Scanner;
public class quadratic
{
/**
* Prompts the user for values of a quadratic equation
* to test functions, quad and squareRoot.
* @param args
* Command line arguments
*/
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String again = "n";
int inA, inB, inC;
//root[0] = i
//root[1] = root one
//root[2] = root two
float[] root = new float[3];
do
{
System.out.println("Ax^2 + Bx + C = 0");
System.out.println("input value for A:");
inA = scan.nextInt();
System.out.println("input value for B:");
inB = scan.nextInt();
System.out.println("input value for C:");
inC = scan.nextInt();
switch (quad(inA, inB, inC, root))
{
case 0:
System.out.println("Two real roots:");
System.out.println("root 1: " + root[1]);
System.out.println("root 2: " + root[2]);
break;
case 1:
System.out.println("One real root: " + root[1]);
break;
case 2:
System.out.println("Two complex roots:");
System.out.println(root[1] + " + " + root[0] + " i");
System.out.println(root[2] + " - " + root[0] + " i");
break;
default:
System.out.println("Error!");
break;
}
System.out.println("\nAgain? (y/n)");
again = scan.next();
}
while(again.equalsIgnoreCase("y") || again.equalsIgnoreCase("yes"));
System.out.println("Bye!");
}
/**
* Estimates the square root of a number.
*
* @param b
* Number to estimate.
* @return
* Estimation of the passed in number's square root.
*/
private static float squareRoot(float b)
{
if (b == 0)
return 0;
float x, old, diff;
diff = 0;
x = b;
do
{
//save old guess
old = x;
//calculate new
x = (x + (b / x)) / 2;
//compare guesses
diff = Math.abs(old - x);
}
while(diff > 0.00001F);
return x;
}
/**
* Using the Quadratic formula, finds the roots of a
* Quadratic equation.
*
* @param a
* Number representing A in the eqation: AX2 + BX + C = 0
* @param b
* Number representing B in the eqation: AX2 + BX + C = 0
* @param c
* Number representing C in the eqation: AX2 + BX + C = 0
* @return
* Returns the type of roots in a status variable;
* 0 for two real roots, 1 for a single real root,
* and 2 for two complex roots.
*/
private static int quad(int a, int b, int c, float[] root)
{
//root[0] = i
//root[1] = root one
//root[2] = root two
int status = 0;
if (a == 0)
{
status = 3;
return status;
}
float descrim;
//determine equation used by the discriminant
descrim = ((b*b) - (4 * (a * c)));
if (descrim == 0)
status = 1;
else
if (descrim < 0)
status = 2;
//calulate once
int twoA = (a * 2);
int negB = (-1 * b);
switch(status)
{
case 0: // two real roots
descrim = squareRoot(descrim);
root[1] = (negB + descrim) / twoA;
root[2] = (negB - descrim) / twoA;
break;
case 1: // one real root
root[1] = (b / twoA) * -1;
break;
case 2: // two complex root
root[0] = (4 * (a * c)) - (b * b);
root[0] = squareRoot(root[0]);
root[0] = root[0] / twoA;
root[1] = negB / twoA;
root[2] = negB / twoA;
break;
default:
break;
}
return status;
}
}
////
///////////////////////////////////////////////////////////
Thanks!
Now let's hope that posting this doesn't completly butchure the whitespace.
Basicaly, just wondering if anyone'd be willing to take a quick peek at this, and clue me in on a fix for the exception PCspim's throwing at me, or offer any usefull comments.
I realy can't see why that function is any different than what's in the higher-level language version. I'm still fairly new to the Computer Science scene (as you'll see below) so hopfully you'll be gentle...:
##################################################
#
#
.text
.globl main
main:
li $v0, 6 #
syscall # load float
mov.s $f12, $f0 #
jal sqroot # test function
mov.s $f12, $f0 #
li $v0, 3 #
syscall # print results
li $v0, 10 #
syscall # exit program
# b in $f12
# ret in $f0 (x)
sqroot:
li $t0, 0 #
mtc1 $t0, $f2 #
cvt.s.w $f16, $f2 # cast floating point zero
mov.s $f4, $f16 # old guess
mov.s $f0, $f16 # x
mov.s $f8, $f16 # difference
c.eq.s $f12, $f16 # if (b == 0)
bc1t return # return 0
li $t0, 2 #
mtc1 $t0, $f2 #
cvt.s.w $f6, $f2 # cast floating point 2
l.s $f18, precision # Guessing precision
loop:
mov.s $f4, $f0 # save old guess
div.s $f10, $f12, $f0 # temp = (b / x)
add.s $f10, $f10, $f0 # temp = (b / x) + x
div.s $f0, $f10, $f6 # x = ((b / x) + x) / 2
sub.s $f10, $f4, $f0 # temp = old - x
abs.s $f8, $f10 # difference = |temp|
c.le.s $f8, $f18 #
bc1f loop #
return:
jr $ra
#
#
#########################################################
////////////////////////////////////////////////////////
/// What everything -should- be working like, in Java.
/**
* @author *Omitted*
* Driver program to test quadratic and square root functions.
*/
import java.util.Scanner;
public class quadratic
{
/**
* Prompts the user for values of a quadratic equation
* to test functions, quad and squareRoot.
* @param args
* Command line arguments
*/
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String again = "n";
int inA, inB, inC;
//root[0] = i
//root[1] = root one
//root[2] = root two
float[] root = new float[3];
do
{
System.out.println("Ax^2 + Bx + C = 0");
System.out.println("input value for A:");
inA = scan.nextInt();
System.out.println("input value for B:");
inB = scan.nextInt();
System.out.println("input value for C:");
inC = scan.nextInt();
switch (quad(inA, inB, inC, root))
{
case 0:
System.out.println("Two real roots:");
System.out.println("root 1: " + root[1]);
System.out.println("root 2: " + root[2]);
break;
case 1:
System.out.println("One real root: " + root[1]);
break;
case 2:
System.out.println("Two complex roots:");
System.out.println(root[1] + " + " + root[0] + " i");
System.out.println(root[2] + " - " + root[0] + " i");
break;
default:
System.out.println("Error!");
break;
}
System.out.println("\nAgain? (y/n)");
again = scan.next();
}
while(again.equalsIgnoreCase("y") || again.equalsIgnoreCase("yes"));
System.out.println("Bye!");
}
/**
* Estimates the square root of a number.
*
* @param b
* Number to estimate.
* @return
* Estimation of the passed in number's square root.
*/
private static float squareRoot(float b)
{
if (b == 0)
return 0;
float x, old, diff;
diff = 0;
x = b;
do
{
//save old guess
old = x;
//calculate new
x = (x + (b / x)) / 2;
//compare guesses
diff = Math.abs(old - x);
}
while(diff > 0.00001F);
return x;
}
/**
* Using the Quadratic formula, finds the roots of a
* Quadratic equation.
*
* @param a
* Number representing A in the eqation: AX2 + BX + C = 0
* @param b
* Number representing B in the eqation: AX2 + BX + C = 0
* @param c
* Number representing C in the eqation: AX2 + BX + C = 0
* @return
* Returns the type of roots in a status variable;
* 0 for two real roots, 1 for a single real root,
* and 2 for two complex roots.
*/
private static int quad(int a, int b, int c, float[] root)
{
//root[0] = i
//root[1] = root one
//root[2] = root two
int status = 0;
if (a == 0)
{
status = 3;
return status;
}
float descrim;
//determine equation used by the discriminant
descrim = ((b*b) - (4 * (a * c)));
if (descrim == 0)
status = 1;
else
if (descrim < 0)
status = 2;
//calulate once
int twoA = (a * 2);
int negB = (-1 * b);
switch(status)
{
case 0: // two real roots
descrim = squareRoot(descrim);
root[1] = (negB + descrim) / twoA;
root[2] = (negB - descrim) / twoA;
break;
case 1: // one real root
root[1] = (b / twoA) * -1;
break;
case 2: // two complex root
root[0] = (4 * (a * c)) - (b * b);
root[0] = squareRoot(root[0]);
root[0] = root[0] / twoA;
root[1] = negB / twoA;
root[2] = negB / twoA;
break;
default:
break;
}
return status;
}
}
////
///////////////////////////////////////////////////////////
Thanks!
Now let's hope that posting this doesn't completly butchure the whitespace.