I have a class definition for a class of rational numbers. My assignment is to be able to add, multiply and divide any fraction I put in my main function. My program can do all that, but I'm having trouble simplifying the fractions. I want to try and use only two methods to simplify, for example public void reduce(); and private static gcd();
public class Rational {
private int num;
private int denom;
public Rational() {
num = 0;
denom = 1;
}
public Rational(int n, int d) {
num = n;
denom = d;
reduce();
}
public Rational plus(Rational t) {
int tnum = 0;
int tdenom = 1;
tnum = (this.num * t.denom) + (this.denom * t.num);
tdenom = (t.denom * this.denom);
Rational r = new Rational (tnum, tdenom);
return r;
}
public Rational minus(Rational t) {
int tnum = 0;
int tdenom = 1;
tnum = (this.num * t.denom) - (this.denom * t.num);
tdenom = (t.denom * this.denom);
Rational r = new Rational (tnum, tdenom);
return r;
}
public Rational multiply(Rational t) {
int tnum = 0;
int tdenom = 1;
tnum = this.num * t.num;
tdenom = t.denom * this.denom;
Rational r = new Rational (tnum, tdenom);
return r;
}
public Rational divide(Rational t) {
int tnum = 0;
int tdenom = 1;
tnum = this.num / t.num;
tdenom = this.denom / t.denom;
Rational r = new Rational (tnum, tdenom);
return r;
}
private static int gcd(int n, int d) {
return gcd(d, n%d);
}
public void reduce() {
//call gcd
gcd(num, denom);
//divide num and denom by gcd by
num = num / gcd(num,denom);
denom = denom / gcd(num,denom);
}
public String toString() {
return String.format("%d/%d", num, denom);
}
}
public class RationalMain {
public static void main(String[] args) {
Rational x = new Rational();
Rational y = new Rational(1,4);
Rational z = new Rational(1,2);
//x = y - z;
x = y.plus(z);
System.out.printf("%s = %s + %s\n", x.toString(), y.toString(), z.toString());
x = z.minus(y);
System.out.printf("%s = %s - %s\n", x.toString(), z.toString(), y.toString());
x = z.multiply(y);
System.out.printf("%s = %s * %s\n", x.toString(), z.toString(), y.toString());
x = y.divide(z);
System.out.printf("%s = %s / %s\n", x.toString(), y.toString(), z.toString());
}
}
That's not how you might achieve the Greatest Common Divisor (GCD). Before you will be able to get your code to work properly you will need to at least fix your gcd() method since currently it will recurse until an ArithmeticException (/ by zero) is generated. You might achieve the task this way:
private static int gcd(int num, int den) {
num = Math.abs(num); // if numerator is signed convert to unsigned.
int gcd = Math.abs(den); // if denominator is signed convert to unsigned.
int temp = num % gcd;
while (temp > 0) {
num = gcd;
gcd = temp;
temp = num % gcd;
}
return gcd;
}
To convert your fractions too their Lowest Terms your reduce() method might look like this if it accepted a Fraction String as an argument (you modify the method parameters if you like):
/*
A Fraction String can be supplied as: "1/2", or "2 1/2", or
"2-1/2, or "-2 32/64", or "-2-32/64". The last 2 examples are
negative fraction values.
*/
private String reduce(String fractionString) {
// Fraction can be supplied as: "1/2", or "2 1/2", or "2-1/2".
// Make sure it's a Fraction String that was supplied as argument...
inputString = inputString.replaceAll("\\s+", " ").trim();
if (!inputString.matches("\\d+\\/\\d+|\\d+\\s+\\d+\\/\\d+|\\d+\\-\\d+\\/\\d+")) {
return null;
}
str2 = new StringBuilder();
String wholeNumber, actualFraction;
if (inputString.contains(" ")) {
wholeNumber = inputString.substring(0, inputString.indexOf(" "));
actualFraction = inputString.substring(inputString.indexOf(" ") + 1);
str2.append(wholeNumber);
str2.append(" ");
}
else if (inputString.contains("-")) {
wholeNumber = inputString.substring(0, inputString.indexOf("-"));
actualFraction = inputString.substring(inputString.indexOf("-") + 1);
str2.append(wholeNumber);
str2.append("-");
}
else {
actualFraction = inputString;
}
String[] tfltParts = actualFraction.split("\\/");
int tfltNumerator = Integer.parseInt(tfltParts[0]);
int tfltDenominator = Integer.parseInt(tfltParts[1]);
// find the larger of the numerator and denominator
int tfltN = tfltNumerator;
int tfltD = tfltDenominator;
int tfltLargest;
if (tfltNumerator < 0) {
tfltN = -tfltNumerator;
}
if (tfltN > tfltD) {
tfltLargest = tfltN;
}
else {
tfltLargest = tfltD;
}
// Find the largest number that divides the numerator and
// denominator evenly
int tfltGCD = 0;
for (int tlftI = tfltLargest; tlftI >= 2; tlftI--) {
if (tfltNumerator % tlftI == 0 && tfltDenominator % tlftI == 0) {
tfltGCD = tlftI;
break;
}
}
// Divide the largest common denominator out of numerator, denominator
if (tfltGCD != 0) {
tfltNumerator /= tfltGCD;
tfltDenominator /= tfltGCD;
}
str2.append(String.valueOf(tfltNumerator)).append("/").append(String.valueOf(tfltDenominator));
return str2.toString();
}
As you can see, a whole number can also be supplied with your fraction so, if you do a call to the above reduce() method like this:
System.out.println(reduce("12-32/64"));
System.out.println(reduce("12 32/64"));
The console window will display:
12-1/2
12 1/2
Related
so I am trying to build this ADT for rational numbers in java but for some reason I keep getting this error that says cannot find symbol when trying to compile. What em I doing wrong? Is this error because of my syntax?
Author: Juan Suarez
// Class : CS1102 ~ java
// Date : 01/30/2018
// Topic : This porblem set focuse on the implemantation of an
// ADT for rational numbers.
public class RationalC implements Rational {
private int num;
private int den;
// ****************** CONSTRUCTOR **********************************
public RationalC (int numerator, int denominator) {
if (this.den == 0){
throw new ArithmeticException("*** WARNING! input non zero denominator");
}
int reduceFraction = gcd(numerator, denominator);
this.num = numerator / reduceFraction;
this.den = denominator / reduceFraction;
if (this.den < 0) {
this.den = this.den * -1;
this.num = this.num * -1;
}
}
//********************* GETTERS ************************************
public int getNumerator() { return this.num; }
public int getDenominator() { return this.den; }
public boolean equal(Rational b) {
boolean
a = this.getNumerator == b.getNumerator;
v = this.getDenominator == b.getDenominator;
return a && v;
}
// ******************* OPERATIONS **********************************
//return this + that
//
public RationalC plus(Rational b) {
int commonDenominator = this.getDenominator() * b.getDenominator();
int num1 = b.getDenominator() * this.getNumerator();
int num2 = b.getNumerator() * this.getDenominator();
int complete = num1 + num2;
return new RationalC (complete, commonDenominator);
}
//returns this - that
//
public RationalC subtract(Rational b) {
int commonDenominator = this.getDenominator() * b.getDenominator();
int num1 = b.getDenominator() * this.getNumerator();
int num2 = b.getNumerator() * this.getDenominator();
int complete = num1 - num2;
return new RationalC (complete, commonDenominator);
}
// return this * that
//
public Rational multiply(Rational b){
int top = this.getNumerator() * b.getNumerator();
int bottom = this.getDenominator() * b.getDenominator();
return new RationalC (top, bottom);
}
//return this / that
//
public Rational divide(Rational b){
int top = this.getNumerator() * b.getDenominator();
int bottom = this.getDenominator() * b.getNumerator();
return new RationalC (top, bottom);
}
//retuns value
//
public boolean equals(Rational b) {
if (num == b.getNumerator() && this.getDenominator() == b.getDenominator() )
return(true);
}
//********************* TOOLS **************************************
//returns the rational number to a string
//
public String toString() {
return "(" + this.num + "," + this.den + ")";
}
//returns -1 , 0, +1 if the value of the rational is <, >, or =
//
public int compareTo(Rational b) {
long leftHand = this.getNumerator() * b.getDenominator();
long rightHand = this.getDenominator() * b.getNumerator();
if (leftHand < rightHand) return -1;
if (leftHand > rightHand) return +1;
return 0;
}
private static int gcd(int m, int n) {
if(m < 0) m = -m;
if(n < 0) n = -n;
return m * (n / gcd(m,n));
}
public Rational reciprical(Rational b){
return new RationalC (this.getDenominator(), this.getNumerator() );
}
//******************* TEST UNIT ************************************
public static void main(String[] args) {
x = new Rational (1,2);
y = new Rational (1,3);
z = x.plus(y);
StdOut.println(z);
}
}
In the below piece of code, you didn't declare local variable v.
public boolean equal(Rational b) {
boolean
a = this.getNumerator == b.getNumerator;
v = this.getDenominator == b.getDenominator;
return a && v;
}
getNumerator and getDenominator are methods.
Call them as this.getNumerator() and this.getDenominator().
Also make sure Rational class is having getNumerator and getDenominator methods.
I have to sort an array of fractions here is my code for the class which is working fine.
public class Fraction implements Comparable<Fraction>{
private int numerator;
private int denominator;
public Fraction(int num, int den){
numerator = num;
denominator = den;
}
public int compareTo(Fraction fraction){
if(decimalValue()>fraction.decimalValue()){
return 1;
}else if(decimalValue()<fraction.decimalValue()){
return -1;
}else{
return 0;
}
}
public Fraction reduce(int numerator, int denominator){
if(numerator==0&&denominator==0){
numerator = 0;
denominator = 0;
}
else{
for(int x = Math.min(Math.abs(numerator), Math.abs(denominator)); x>0; x--){
if(denominator == numerator){
numerator = 1;
denominator = 1;
}
else if(numerator == 0){
numerator = 0;
denominator = 1;
}
else if(numerator%x==0 && denominator%x==0){
numerator = numerator/x;
denominator = denominator/x;
}
}
}
public double decimalValue(){
double decimal = (numerator*1.0)/(1.0*denominator);
return decimal;
}
public String toString(){
reduce(numerator, denominator);
return ((numerator) + "/" + (denominator));
}
}
For some reason the sort() is not working if I used it with a comparator like in the answer it works but I don't understand why this doesn't work. Here is the tester:
public class FractionChecker{
public static void main (String[]args){
int n, d;
Random rand = new Random();
Fraction[] f = new Fraction [20];
for (int j= 0; j<20; j++){
n = rand.nextInt(20);
d = rand.nextInt(19)+1;
f[j] = new Fraction (n,d);
}
System.out.println("Unsorted " + Arrays.toString(f));
Arrays.sort(f);
}
}
Error:
----jGRASP exec: java FractionChecker
Unsorted [7/6, 15/14, 5/15, 8/9, 19/16, 16/5, 11/16, 2/9, 11/10, 10/12, 12/11, 9/18, 15/4, 11/4, 10/7, 12/8, 13/14, 19/5, 19/15, 13/5]
Exception in thread "main" java.lang.ClassCastException: Fraction cannot be cast to java.lang.Comparable
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.sort(Arrays.java:1079)
at FractionChecker.main(FractionChecker.java:18)
----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
This is the error I am getting when I use Arrays.sort(f) and I am not sure why.
Collections.sort expects a List whose type implements Comparable. You're providing an array of Fraction objects instead.
You should use Arrays.sort instead:
Arrays.sort(f);
If the above throws a ClassCastException for some reason, you can try this version of Arrays.sort, which requires a Comparator as an argument that will do the comparing:
Arrays.sort(f, new java.util.Comparator<Fraction>() {
#Override
public int compare(Fraction f1, Fraction f2) {
return f1.compareTo(f2);
}
});
I don't see your decimalValue code. A better way to compare fractions in any case may be:
long left = numerator * other.denominator;
long right = other.numerator * denominator;
if (left == right) {
return 0;
} else if (left < right) {
return -1;
} else /* if (left > right) */ {
return 1;
}
Not that this will increase your performance drastically but it is just neater and handles division by zero in a sensible way.
The code below works and prints success
import java.util.Arrays;
public class Fraction implements Comparable<Fraction>{
private int numerator;
private int denominator;
public Fraction(int num, int den){
numerator = num;
denominator = den;
}
public int compareTo(Fraction fraction){
if(decimalValue()>fraction.decimalValue()){
return 1;
}else if(decimalValue()<fraction.decimalValue()){
return -1;
}else{
return 0;
}
}
public double decimalValue(){
double decimal = (numerator*1.0)/(1.0*denominator);
return decimal;
}
public String toString(){
return ((numerator) + "/" + (denominator));
}
public static void main(String[] a) {
Fraction[] fractions = new Fraction[2];
fractions[0] = new Fraction(1,1);
fractions[1] = new Fraction(2,3);
Arrays.sort(fractions);
System.out.println("success");
}
}
So I have a List<Number> that contains both Doubles and Integers, I have to loop through the List and randomize each Number by +- 25%
This is an example similar to my code:
public class ListTester {
public static void main(String[] args){
ListTester lt = new ListTester();
}
public ListTester(){
int a = 1;
int b = 2;
double d = 3.5;
randomizableStats = new ArrayList(Arrays.asList(a, b, d));
randomizeStats();
}
protected List <Number> randomizableStats;
protected void randomizeStats(){
List<Number> randomizedStats = new ArrayList<>();
Random r = new Random();
for (Number n : randomizableStats){
int l = r.nextInt(1);
int i = r.nextInt(25);
if (l == 1){
if (n instanceof Integer) {
Integer n2 = (Integer) n;
n2 = n2 + (i / 100 * n2) + 1;
randomizedStats.add(n2);
} else if (n instanceof Double) {
Double n2 = (Double) n;
double d = (double) i;
n2 = n2 + (d / 100 * n2);
randomizedStats.add(n2);
}
} else {
if (n instanceof Integer) {
Integer n2 = (Integer) n;
n2 = n2 - (i / 100 * n2) - 1;
randomizedStats.add(n2);
} else if (n instanceof Double) {
Double n2 = (Double) n;
double d = (double) i;
n2 = n2 - (d / 100 * n2);
randomizedStats.add(n2);
}
}
}
for (Number n : randomizableStats){
System.out.print(n);
}
for (Number n : randomizedStats){
System.out.print(n);
}
}
}
The question is, is there a better way to write this, without having to go through a forced casting to Integer\Double complicating everything with additional if statements?
If you don't mind converting everything to double, you can use Number.doubleValue().
for (Number n : randomizables) {
int i = r.nextInt(1);
int l = r.nextInt(24) + 1;
if (i == 1) {
double d = n.doubleValue();
d = d + (d * l/100);
// do something with d
}
}
Well, as a design exercise one solution to avoid using instaceof, which in general is discourage (I'll comment on that latter), one solution is to use polymorphism. In your case for instance, you can create the following classes:
public abstract class NewNumber {
protected Number number;
public abstract NewNumber multiply(int m);
public Number toNumber() {
return this.number;
}
}
public class NewNumberInt extends NewNumber{
public NewNumberInt(Integer number) {
this.number = number;
}
#Override
public NewNumber multiply(int m) {
int out = this.number.intValue()*m;
return new NewNumberInt(out);
}
}
public class NewNumberDou extends NewNumber{
public NewNumberDou(Double number) {
this.number = number;
}
#Override
public NewNumber multiply(int m) {
double out = this.number.doubleValue()*m;
return new NewNumberDou(out);
}
}
With this, your program can be rewritten in the following way
int a = 1;
int b = 2;
double d = 3.5;
List<NewNumber> randomizables = new ArrayList<NewNumber>();
randomizables.add(new NewNumberInt(a));
randomizables.add(new NewNumberInt(b));
randomizables.add(new NewNumberDou(d));
// Example on how to convert from NewNumber to Number
List<Number> randomizablesNumber = new ArrayList<Number>();
for (NewNumber numNew:randomizables) randomizablesNumber.add(numNew.toNumber());
public void randomize(){
Random r = new Random();
for (NewNumber n : randomizables){
int i = r.nextInt(1);
int l = r.nextInt(24)+1;
if (i == 1){
NewNumber n2 = n.multiply(l/100);
}
}
}
The question is whether it is worth it to write all this just to avoid the usage of instanceof. From a design point of view of course it is and I personally prefer to spend 2 more minutes but have something well done, than just save these minutes and have something that is not nice. However, I understand that sometimes simple is best...
This is my program
// ************************************************************
// PowersOf2.java
//
// Print out as many powers of 2 as the user requests
//
// ************************************************************
import java.util.Scanner;
public class PowersOf2 {
public static void main(String[] args)
{
int numPowersOf2; //How many powers of 2 to compute
int nextPowerOf2 = 1; //Current power of 2
int exponent= 1;
double x;
//Exponent for current power of 2 -- this
//also serves as a counter for the loop Scanner
Scanner scan = new Scanner(System.in);
System.out.println("How many powers of 2 would you like printed?");
numPowersOf2 = scan.nextInt();
System.out.println ("There will be " + numPowersOf2 + " powers of 2 printed");
//initialize exponent -- the first thing printed is 2 to the what?
while( exponent <= numPowersOf2)
{
double x1 = Math.pow(2, exponent);
System.out.println("2^" + exponent + " = " + x1);
exponent++;
}
//print out current power of 2
//find next power of 2 -- how do you get this from the last one?
//increment exponent
}
}
The thing is that I am not allowed to use the math.pow method, I need to find another way to get the correct answer in the while loop.
Powers of 2 can simply be computed by Bit Shift Operators
int exponent = ...
int powerOf2 = 1 << exponent;
Even for the more general form, you should not compute an exponent by "multiplying n times". Instead, you could do Exponentiation by squaring
Here is a post that allows both negative/positive power calculations.
https://stackoverflow.com/a/23003962/3538289
Function to handle +/- exponents with O(log(n)) complexity.
double power(double x, int n){
if(n==0)
return 1;
if(n<0){
x = 1.0/x;
n = -n;
}
double ret = power(x,n/2);
ret = ret * ret;
if(n%2!=0)
ret = ret * x;
return ret;
}
You could implement your own power function.
The complexity of the power function depends on your requirements and constraints.
For example, you may constraint exponents to be only positive integer.
Here's an example of power function:
public static double power(double base, int exponent) {
double ans = 1;
if (exponent != 0) {
int absExponent = exponent > 0 ? exponent : (-1) * exponent;
for (int i = 1; i <= absExponent; i++) {
ans *= base;
}
if (exponent < 0) {
// For negative exponent, must invert
ans = 1.0 / ans;
}
} else {
// exponent is 0
ans = 1;
}
return ans;
}
If there are no performance constraints you can do:
double x1=1;
for(int i=1;i<=numPowersOf2;i++){
x1 =* 2
}
You can try to do this based on this explanation:
public double myPow(double x, int n) {
if(n < 0) {
if(n == Integer.MIN_VALUE) {
n = (n+1)*(-1);
return 1.0/(myPow(x*x, n));
}
n = n*(-1);
return (double)1.0/myPow(x, n);
}
double y = 1;
while(n > 0) {
if(n%2 == 0) {
x = x*x;
}
else {
y = y*x;
x = x*x;
}
n = n/2;
}
return y;
}
It's unclear whether your comment about using a loop is a desire or a requirement. If it's just a desire there is a math identity you can use that doesn't rely on Math.Pow.
xy = ey∙ln(x)
In Java this would look like
public static double myPow(double x, double y){
return Math.exp(y*Math.log(x));
}
If you really need a loop, you can use something like the following
public static double myPow(double b, int e) {
if (e < 0) {
b = 1 / b;
e = -e;
}
double pow = 1.0;
double intermediate = b;
boolean fin = false;
while (e != 0) {
if (e % 2 == 0) {
intermediate *= intermediate;
fin = true;
} else {
pow *= intermediate;
intermediate = b;
fin = false;
}
e >>= 1;
}
return pow * (fin ? intermediate : 1.0);
}
// Set the variables
int numPowersOf2; //How many powers of 2 to compute
int nextPowerOf2 = 1; //Current power of 2
int exponent = 0;
/* User input here */
// Loop and print results
do
{
System.out.println ("2^" + exponent + " = " + nextPowerOf2);
nextPowerOf2 = nextPowerOf2*2;
exponent ++;
}
while (exponent < numPowersOf2);
here is how I managed without using "myPow(x,n)", but by making use of "while". (I've only been learning Java for 2 weeks so excuse, if the code is a bit lumpy :)
String base ="";
String exp ="";
BufferedReader value = new BufferedReader (new InputStreamReader(System.in));
try {System.out.print("enter the base number: ");
base = value.readLine();
System.out.print("enter the exponent: ");
exp = value.readLine(); }
catch(IOException e){System.out.print("error");}
int x = Integer.valueOf(base);
int n = Integer.valueOf(exp);
int y=x;
int m=1;
while(m<n+1) {
System.out.println(x+"^"+m+"= "+y);
y=y*x;
m++;
}
To implement pow function without using built-in Math.pow(), we can use the below recursive way to implement it. To optimize the runtime, we can store the result of power(a, b/2) and reuse it depending on the number of times is even or odd.
static float power(float a, int b)
{
float temp;
if( b == 0)
return 1;
temp = power(a, b/2);
// if even times
if (b%2 == 0)
return temp*temp;
else // if odd times
{
if(b > 0)
return a * temp * temp;
else // if negetive i.e. 3 ^ (-2)
return (temp * temp) / a;
}
}
I know this answer is very late, but there's a very simple solution you can use if you are allowed to have variables that store the base and the exponent.
public class trythis {
public static void main(String[] args) {
int b = 2;
int p = 5;
int r = 1;
for (int i = 1; i <= p; i++) {
r *= b;
}
System.out.println(r);
}
}
This will work with positive and negative bases, but not with negative powers.
To get the exponential value without using Math.pow() you can use a loop:
As long as the count is less than b (your power), your loop will have an
additional "* a" to it. Mathematically, it is the same as having a Math.pow()
while (count <=b){
a= a* a;
}
Try this simple code:
public static int exponent(int base, int power) {
int answer = 1;
for(int i = 0; i < power; i++) {
answer *= base;
}
return answer;
}
I have two methods: power and factorial:
public static long pow(int x, int n) {
long p = x;
for (int i = 1; i < n; i++) {
p *= x;
}
return p;
}
public static long fact(int n) {
long s = n;
for (int i = 1; i < n; i++ ) {
s *= i;
}
return s;
}
that are returning longs. When I want to use them in new method evaluating Exponential function i get wrong results comparing to Math.exp(x). My code:
public static void exp(int x, double eps) {
int i = 1;
double pow = 1.0;
double fact = 1.0;
double sum = 0.0;
double temp;
do {
temp = pow/fact;
sum += temp;
pow = pow(x, i);
fact = fact(i);
i++;
}
while (temp > eps);
System.out.println("Check: " + Math.exp(x));
System.out.println("My: " + sum);
}
public static void main() {
int x = 10;
double eps = 0.0000000000001;
exp(x, eps);
}
and the output for x=10 is:
Check: 22026.465794806718
My: 21798.734894914145
the larger x, the bigger "loss of precision" (not exactly, because you can't really call it precise...).
The twist is, when methods power and factorial return double then the output is correct. Can anyone explain me how to make it work?
Methods pow and fact must return long and I must use them in exp (college assignment).
If you try this pow method:
public static long pow(int x, int n) {
long p = x;
System.out.println("Pow: "+x+","+n);
for (int i = 1; i < n; i++) {
p *= x;
System.out.println(p);
}
return p;
}
You get this output:
...
Pow: 10,20
100
1000
10000
...
...
1000000000000000
10000000000000000
100000000000000000
1000000000000000000
-8446744073709551616
7766279631452241920
The long value overflows: 10^20 is just too big to fit in a long.
Methods pow and fact must return long and I must use them in exp (college assignment).
Then there is not much you can do to fix it. You could throw an exception if eps is too small.
How large is x typically? It could be integer overflow. Try changing all the int arguments in pow and fact to be long instead.
Long data types can't handle decimal precision that's why you're values are wrong with long. Why don't you just have the functions return the double values?
Edit: Heres what I came up with:
public static long pow(int x, int n)
{
double p = x;
for (int i = 1; i < n; i++) {
p *= x;
}
return (long)p;
}
public static long fact(int n)
{
double s = n;
for (int i = 1; i < n; i++ ) {
s *= i;
}
return (long)s;
}
public static void exp(int x, double eps)
{
double pow = 1.0;
double fact = 1.0;
double sum = 0.0;
double temp;
for(int ii=1; ii < 100; ii++)
{
pow = pow(x, ii);
fact = fact(ii);
temp = (double)pow/(double)fact;
temp = temp == 1 ? 0 : temp;
sum += temp;
}
System.out.println("Check: " + Math.exp(x));
System.out.println("My: " + sum);
}
public static void main(final String[] args)
{
int x = 10;
double eps = 0.0000000000001;
exp(x, eps);
}
That's about the closest you're going to get without using the decimals.
Check: 22026.465794806718
My: 21946.785573087538