Polynomial Add: Unable to run code - java

This program is on Eclipse. I have to declare variables "Integer" not "int". When I am compiling this code shows no error but there is a runtime error. Please fix this problem.
Runtime error:
Exception in thread "main" java.lang.NullPointerException
at Polynomial.add(Polynomial.java:17)
at PolynomialTest.main(PolynomialTest.java:7)
Polynomial.java
public class Polynomial{
Integer coef[];
Integer exp;
public Polynomial(Integer a, Integer b) {
coef = new Integer[b+1];
coef[b] = a;
exp = b;
}
// return c = a + b
public Polynomial add(Polynomial b) {
Polynomial a = this;
Polynomial c= new Polynomial(0, Math.max(a.exp, b.exp));
for (Integer i = 0; i <= a.exp; i++){
c.coef[i] = c.coef[i] + a.coef[i];
}
for (int i = 0; i <= b.exp; i++){
c.coef[i] += b.coef[i];
}
return c;
}
public String toString() {
if (exp == 0){
return "" + coef[0];
}else
if (exp == 1){
return coef[1] + "x + " + coef[0];
}
String s = coef[exp] + "x^" + exp;
for (int i = exp-1; i >= 0; i--) {
if (coef[i] == 0){
continue;
}
else if (coef[i] > 0){
s = s + " + " + ( coef[i]);
}
if (i == 1){
s = s + "x";
}
else
if (i > 1) {
s = s + "x^" + i;
}
}
return s;
}}
PolynomialTest.java
public class PolynomialTest {
// test client
public static void main(String[] args) {
Polynomial p1 = new Polynomial(4, 4);
Polynomial p2 = new Polynomial(7, 2);
Polynomial p3 = new Polynomial(3, 0);
Polynomial p = p1.add(p2).add(p3); // 4x^3 + 3x^2 + 1
Polynomial q1 = new Polynomial(2, 2);
Polynomial q2 = new Polynomial(5, 4);
Polynomial q = q1.add(q2);
System.out.println("p(x) = " + p);
System.out.println("q(x) = " + q);
System.out.println("p(x) + q(x) = " + p.add(q));
}
}

Since Integer is an Object, you will need to initialize each entry in your coef array with a new Integer object.
You could just do this in the Polynomial constructor:
public class Polynomial{
Integer coef[];
Integer exp;
public Polynomial(Integer a, Integer b) {
coef = new Integer[b+1];
for (int i = 0; i < coef.length; i++){
coef[i] = new Integer(0); //create a new Integer and initialize to zero
}
coef[b] = a;
exp = b;
}

Related

How do you return multiple values from a for loop in Java using a return statement?

I would like to send multiple values from my getMultiples method to my main method using a return statement and no print or println statements.
public class StaticMethods {
public static void main (String[] args) {
int a = 6;
int b = 9;
int result = getMultiple(a,b);
System.out.println(result + "\n")
System.out.println("The first " + a + " multiples of " + b + " are: ");
int p = getMultiples(a,b);
}
public static int getMultiple(int a,int b) {
return (int) (a * b);
}
public static int getMultiples(int a, int b) {
int p = 0;
for (int i = 1; i <= a; i++) {
p = getMultiple(a,i);
}
return (p);
}
}
I have tried putting the return statement in the for loop but it does not work.
In Java as soon as return is encountered in the code, method is removed from execution stack and flow is returned back to calling method. So you can not return multiple values from a method. Rather you should create a list/array and return that as below(array example):
public class StaticMethods {
public static void main (String[] args) {
int a = 6;
int b = 9;
int result = getMultiple(a,b);
System.out.println(result + "\n");
System.out.println("The first " + a + " multiples of " + b + " are: ");
int p[] = getMultiples(a,b);
}
public static int getMultiple(int a,int b) {
return (int) (a * b);
}
public static int[] getMultiples(int a, int b) {
int[] p = new int[a];
for (int i = 1; i <= a; i++) {
p[i-1] = getMultiple(a,i);
}
return p;
}
}

Class is not abstract and does not override abstract method error [duplicate]

This question already has answers here:
Class is not abstract and does not override abstract method
(2 answers)
Closed 3 years ago.
I've been working on a program that can find the root of a polynomial for school, and do various other polynomial related stuff like adding them together or finding the value of a polynomial with a given x. While the other two classes I made work fine (they find the root of a sin and cos function), my FuncPoly class seems to be conflicting with my evaluate method somehow, and doesn't want to inherit it. This is my exact error message:
.\FuncPoly.java:1: error: FuncPoly is not abstract and does not override abstract method evaluate(double) in Function
public class FuncPoly extends Function{
I've tried fiddling with the evaluate method a little bit and trying to add some overrides in, but it hasn't been helping me very much. I need help finding what is causing this error; maybe its related to my constructor?. Thanks for reading and your help! Code is below; there is a lot in there, but I don't think most of it is pertinent to the question.
public abstract double evaluate(double x); //Basically just a filler for SinFunc and CosFunc
public double findRoot(double a, double b, double epsilon){
double x = ( a + b ) / 2;
if (Math.abs( a - x) <= epsilon){
return x;
}else if (evaluate(x)*evaluate(a) >= 0){
return findRoot(x, b, epsilon);
}else{
return findRoot(a, x, epsilon);
}
}
public static void main(String[] args){
//Tests SinFunc and CosFunc
SinFunc q = new SinFunc();
CosFunc w = new CosFunc();
System.out.println("The root of sin(x) with the numbers entered with the given epsilon is: " + q.findRoot(3,4,.00000001));
System.out.println("The root of cos(x) with the numbers entered with the given epsilon is: " + w.findRoot(1,3,.00000001));
//Tests the FuncPoly stuff
int[] test1 = {1,0,-3};
int[] test2 = {1,-1,-2};
FuncPoly poly1 = new FuncPoly(test1);
FuncPoly poly2 = new FuncPoly(test2);
System.out.println("The root of x^2 + (-3) is" + poly1.findRoot(0,10,.00000001));
}
}
____________________________
public class FuncPoly extends Function{
public int coefficients[];
public FuncPoly(int[] coefficients){
//Constructor
this.coefficients = coefficients;
}
public int degree(){
//Finds the highest power by finding the location of the last number in the array.
return this.coefficients.length - 1;
}
public String toString(){
//Converts a polynomial to a string
StringBuilder poly = new StringBuilder();
for(int i = degree(); i >= 0; i--){
if (i == degree()){
poly.append(this.coefficients[i] + "x^" + degree());
}else{
if (this.coefficients[i] == 0){
System.out.println(i);
}else if (i == 0){
poly.append(" + " + this.coefficients[0]);
} else if ( i == 1){
poly.append(" + " + this.coefficients[1] + "x");
} else{
poly.append(" + " + this.coefficients[i] + "x^" + i);
}
}
}
return poly.toString();
}
public FuncPoly add(FuncPoly a){
//Adds the selected polynomial and the last called polynomial together and returns the array.
if (this.degree() > a.degree()){
int[] polyAdd = new int[this.degree() + 1];
for (int i = 0; i <= a.degree(); i++){
polyAdd[i] = a.coefficients[i] + this.coefficients[i];
}
for (int i = a.degree() + 1; i < this.degree() + 1; i++){
polyAdd[i] = this.coefficients[i];
}
FuncPoly polyResult = new FuncPoly(polyAdd);
return polyResult;
} else if (this.degree() < a.degree()){
int[] polyAdd = new int[a.degree() + 1];
for (int i = 0; i <= this.degree(); i++){
polyAdd[i] = a.coefficients[i] + this.coefficients[i];
}
for (int i = this.degree() + 1; i < degree() + 1; i++){
polyAdd[i] = a.coefficients[i];
}
FuncPoly polyResult = new FuncPoly(polyAdd);
return polyResult;
} else {
int[] polyAdd = new int[a.degree() + 1];
for (int i = 0; i < a.degree() + 1; i++){
polyAdd[i] = a.coefficients[i] + this.coefficients[i];
}
FuncPoly polyResult = new FuncPoly(polyAdd);
return polyResult;
}
}
public double value(double x){
//Finds the value of polynomial with a given x.
double sum = 0;
for(int i = 0; i < this.degree() + 1; i++){
sum += this.coefficients[i] * Math.pow(x,i);
}
return sum;
}
}
As far as I can tell, your value() method in FuncPoly needs to be renamed to evaluate() to successfully implement the abstract evaluate() method from the Function class.

Can't find array even though object is of array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
So I'm trying to add 2 polynomials together, and I have created object type Polynomial of ArrayBasedPoly, and that object is an array of the coeff, and the degree is starting from 0.
Ex: [1, 0, 3, 4] is 4x^3 + 3x^2 + 1
In my add class, I'm trying to add the 2 arrays together ->
Ex: p1 = [1, 0, 3, 4], p2 = [-2, -5], Sum[] = [-1, -5, 3, 4]
However, in the add method, it won't recognize p2 as an array, even though the object is an array.
EDIT: Now I know that I need a method to find the length of the array of object p, however, even when I have a separate method, it still can't find it?
public class ArrayBasedPoly extends Polynomial
{
private double [] poly;
public ArrayBasedPoly(double [] poly)
{
this.poly = poly;
}
public ArrayBasedPoly(double coeff, int expon)
{
super.coeff = coeff;
super.expon = expon;
}
public ArrayBasedPoly add(ArrayBasedPoly p)
{
double [] temp = new double [Math.max(p.length, poly.length)]; //<=== Here
return null; //temp
}
public String toString()
{
String s = "";
if (poly == null)
{
return super.toString(); // no poly array
}
for(int i = poly.length - 1; i >= 0; i--)
{
if (i != poly.length - 1 && poly[i] > 0) //adds + sign
{
s += " + ";
}
if (poly[i] != 0) // ignores the 0 in the array
{
if (i == 1) //if expon is 1, doesn't do the ^1
{
s += poly[i] + "x";
} else if (i > 0) { // normal
s += poly[i] + "x^" + i;
} else {
s += poly[i]; // if expon = 0, just prints out the coeff
}
}
}
return s;
}
public static void main (String [] args)
{
double [] c = {1, 0, 3, 4};
double [] c1 = {-2, -5};
Polynomial p1 = new ArrayBasedPoly (c);
System.out.println("p1(x) = " + p1);
Polynomial p2 = new ArrayBasedPoly (c1);
System.out.println("p2(x) = " + p2);
Polynomial p3 = new ArrayBasedPoly(-4, 1);
System.out.println("p3(x) = " + p3);
Polynomial p = p1.add(p2);//.add(p2);
System.out.println("p(x) = " + p);
Polynomial p4 = p.subtract(p3);
System.out.println("p4(x) = " + p4);
Polynomial p5 = p4.getDerivative();
System.out.println("p5(x) = " + p5);
System.out.println("p5(0) = " + p5.evaluate(0));
System.out.println("p5(1) = " + p5.evaluate(1));
}
} //ArrayBasedPoly
public class Polynomial
{
protected double coeff;
protected int expon;
public Polynomial()
{
coeff = 1;
expon = 1;
}
public Polynomial(double coeff, int expon)
{
this.coeff = coeff;
this.expon = expon;
}
public Polynomial(Polynomial p)
{
this.coeff = p.coeff;
this.expon = p.expon;
}
public int getDegree()
{
return expon;
}
public double getCoeff()
{
return coeff;
}
public double evaluate(double x)
{
return Math.pow(x, expon) * coeff;
}
public Polynomial add (Polynomial p)
{
Polynomial temp = new Polynomial();
if (expon != p.expon)
{
return null;
}
temp.coeff = coeff + p.coeff;
temp.expon = expon;
return temp;
} //add
public Polynomial subtract (Polynomial p)
{
Polynomial temp = new Polynomial();
if (expon != p.expon)
{
return null;
}
temp.coeff = coeff - p.coeff;
temp.expon = expon;
return temp;
} //subtract
public Polynomial getDerivative()
{
Polynomial temp = new Polynomial(coeff, expon);
if (expon == 1)
{
temp.coeff = coeff;
temp.expon = 0;
return temp;
}
temp.coeff *= temp.expon;
temp.expon--;
return temp;
} //derivative
public String toString()
{
if (expon == 0)
{
return coeff + "";
}
if (expon == 1)
{
return coeff + "x";
}
return coeff + "x^" + expon;
} //toString
public static void main (String [] args)
{
//Coefficient, Exponent
Polynomial p1 = new Polynomial(3, 2); //3x^2
System.out.println("P1: " + p1);
System.out.println();
System.out.println("Derivative of P1: " + p1.getDerivative());
System.out.println();
System.out.println("P1 if x=2: " + p1.evaluate(2));
Polynomial p2 = new Polynomial(2, 2);
System.out.println();
System.out.println("P2: " + p2);
System.out.println();
System.out.println("P1+P2: " + p1.add(p2));
System.out.println();
System.out.println("P1-P2: " + p1.subtract(p2));
Polynomial p3 = new Polynomial(2, 1);
System.out.println();
System.out.println("P3: " + p3);
System.out.println();
System.out.println("Derivative of P3: " + p3.getDerivative());
System.out.println();
System.out.println("P1+P3: " + p1.add(p3));
} //Main
} //Polynomial
/*
----jGRASP exec: java Polynomial
P1: 3.0x^2
Derivative of P1: 6.0x^1
P1 if x=2: 12.0
P2: 2.0x^2
P1+P2: 5.0x^2
P1-P2: 1.0x^2
P3: 2.0x^1
Derivative of P3: 1.0x^0
P1+P3: null
----jGRASP: operation complete.
*/
It seems like I have made an error, and that the "add" method should
have the parameter "Polynomial" instead because in the main method the
object are of type Polynomial. However, when I use the Polynomial
parameter and have a getLength() method, it still can't find it. –
Agramon
The problem is in the method call:
Polynomial p = p1.add(p2);//.add(p2);
because p1 is a Polynomial it will check THAT class for the add method. You need to cast p1. And then you would need to cast p2 since the method is expecting an ArrayBasedPoly.
Polynomial p = ((ArrayBasedPoly) p1).add((ArrayBasedPoly) p2);
You have multiple issues in this piece of code.
First, ArrayBasedPoly should not be extending Polynomial, as it is not a subclass of it, but an array representation of it. To solve this issue, you should keep an array/list of Polynomial inside ArrayBasedPoly like this:
public class ArrayBasedPoly {
Polynomial[] polyArray;
// or alternatively: List<Polynomial> polyArray;
// or even better, use a sorted structure such as skiplist
}
Second, as Pereira pointed out, you are mixing up these two classes.
The Polynomial class does not contain add().
You can only add two ArrayBasePoly.

Referring to an array in a subclass

So im writing a program to add and subtract Polynomials. The Polynomial is comes in as a String (example: 4x^7-2x^5+3x^2+78) and its split up into Terms (example 4x^7) and then the coefficient value is assigned to PolynomialArray[exponent].
This is part one of my assignment so I have an Interface that was given to me below:
public interface PolynomialInterface {
PolynomialInterface add(PolynomialInterface other);
// Effect: Adds value to owner of addPolynomial method.
// Postcondition: Return value = this + value.
PolynomialInterface subtract(PolynomialInterface other);
// Effect: Subtracts value from owner of addPolynomial method.
// Postcondition: Return value = this - value.
void readPolynomial();
// Postcondition: polynomial read.
String toString();
// Postcondition: polynomial converted to string.
}
Heres my code so far:
import java.lang.*;
public class ArrayWithExponentAsIndexPolynomial implements PolynomialInterface {
Integer PolynomialArray[] = new Integer[1000];
CharSequence minus = "-";
CharSequence plusMinus = "+-";
boolean FirstElementPos = true;
public ArrayWithExponentAsIndexPolynomial(String input) {
if (input.charAt(0) == '-') {
input = input.substring(1);
FirstElementPos = false;
}
String inputPolynomial = input.replaceAll("-", "+-");
// input.replace(minus, plusMinus);
System.out.println(inputPolynomial);
String[] splitTerms = inputPolynomial.split("\\+");
// int PolynomialArray[] = new int[100];
for (int i = 0; i <= splitTerms.length - 1; i++) {
System.out.println(splitTerms[i]);
}
String tempTemp = splitTerms[1];
int coef;
int exponent;
String tempExp = null;
for (int i = 0; i < splitTerms.length; i++) {
String tempTerm = splitTerms[i];
System.out.println();
System.out.println("Term we are working with " + tempTerm);
boolean tempPos = true;
if (tempTerm.contains("-")) {
tempTerm = tempTerm.substring(1);
System.out.println("After removing negative from term: "
+ tempTerm);
tempPos = false;
}
int IndexOfexponent = tempTerm.indexOf('^');
if (IndexOfexponent == -1) {
exponent = 1;
// FirstElementPos = true;
} else {
tempExp = tempTerm.substring(IndexOfexponent + 1);
exponent = Integer.parseInt(tempExp);
}
System.out.println("The exp is " + exponent);
// String tempTerm = splitTerms[i];
System.out.println("The term rn is: " + tempTerm);
String tempTermNoCarrot = tempTerm.replaceAll("\\^" + tempExp, "");
String tempCoef = tempTermNoCarrot.replaceAll("x", "");
// String tempCoef = tempTermNoX.replaceAll(tempExp, "");
System.out.println("THe Coeff rn is: " + tempCoef);
coef = Integer.parseInt(tempCoef);
if (tempPos == false || FirstElementPos == false) {
coef = (coef * -1);
}
System.out.println("After everything, Coef is:" + coef
+ " and exp is: " + exponent);
PolynomialArray[exponent] = coef;
}
}
public PolynomialInterface add(PolynomialInterface other) {
String finalOutput=null;
//Integer top = this.PolynomialArray[i];
Integer Sum[] = new Integer[100];
for (int i = 99; i >= 1; i--){
Integer top = this.PolynomialArray[i];
Integer bottom = other.PolynomialArray[i];
Sum[i] = top + bottom;
}
String tempOutput = null;
for (int i = 99; i >= 1; i--) {
if (Sum[i] != null && Sum[i] != 0) {
tempOutput += "+";
int outputCoef = Sum[i];
tempOutput += outputCoef;
tempOutput += "x^";
tempOutput += i;
}
}
String RemoveNull = tempOutput;
tempOutput = RemoveNull.replaceAll("null", "");
if (tempOutput.charAt(0) == '+') {
tempOutput = tempOutput.substring(1);
}
tempOutput = tempOutput.replaceAll("\\+-","-");
finalOutput = tempOutput;
return new ArrayWithExponentAsIndexPolynomial(finalOutput);
}
public PolynomialInterface subtract(PolynomialInterface other) {
return other;
}
public void readPolynomial() {
}
public String toString() {
String output = null;
for (int i = 99; i >= 1; i--) {
if (PolynomialArray[i] != null && PolynomialArray[i] != 0) {
output += "+";
int outputCoef = PolynomialArray[i];
output += outputCoef;
output += "x^";
output += i;
}
}
String outputTemp = output;
output = outputTemp.replaceAll("null", "");
if (output.charAt(0) == '+') {
output = output.substring(1);
}
output = output.replaceAll("\\+-","-");
return output;
}
}
My question is in the add mehthod, how do i refer to the PolynomialArray in the "other" object. When i do other.PolynomialArray[i] it says PolynomialArray cannot be resolved or is not a field sense in the Interface, there exists no such thing. Id there a way to refer to my intended target without changing the interface because in my future project I will need to use this
Sorry if I'm not being clear. This is my first time posting :)
*quick edit. I'm not done with my code so there are a few place holders here and there and some random print statements
Integer PolynomialArray[] = new Integer[1000];
This is something which your implementing Class ArrayWithExponentAsIndexPolynomial has added. It's not specified in your interface contract. You are trying to get the PolynomialArray[] from your interface reference. That won't work. You need to cast it like ((ArrayWithExponentAsIndexPolynomial)other).PolynomialArray[i];
Simply put, the PolynomialArray field is defined in your ArrayWithExponentAsIndexPolynomial class and unknown to the given PolynomialInterface inferface. In practice, only methods are defined in an interface, not fields (as Amit.rk3 mentioned, static final fields are allowed, though no solution to your problem). A way to access a field of an object indentified solely by an interface, is to define a getSomeField() method in the interface.
I'm not sure if your assignment allows you to add getPolynomialArray() to the given interface, but that would be the simplest solution.
Otherwise, though less elegant, you can cast the given object to your class and access the field directly.

what is wrong with this code when dealing with large values of "long"?

I wrote an utility class to encode numbers in a custom numeral system with base N. As any self-respecting Java programmer I then wrote a unit test to check that the code works as expected (for any number I could throw at it).
It turned out, that for small numbers, it worked. However, for sufficiently large numbers, the tests failed.
The code:
public class EncodeUtil {
private String symbols;
private boolean isCaseSensitive;
private boolean useDefaultSymbols;
private int[] symbolLookup = new int[255];
public EncodeUtil() {
this(true);
}
public EncodeUtil(boolean isCaseSensitive) {
this.useDefaultSymbols = true;
setCaseSensitive(isCaseSensitive);
}
public EncodeUtil(boolean isCaseSensitive, String symbols) {
this.useDefaultSymbols = false;
setCaseSensitive(isCaseSensitive);
setSymbols(symbols);
}
public void setSymbols(String symbols) {
this.symbols = symbols;
fillLookupArray();
}
public void setCaseSensitive(boolean isCaseSensitive) {
this.isCaseSensitive = isCaseSensitive;
if (useDefaultSymbols) {
setSymbols(makeAlphaNumericString(isCaseSensitive));
}
}
private void fillLookupArray() {
//reset lookup array
for (int i = 0; i < symbolLookup.length; i++) {
symbolLookup[i] = -1;
}
for (int i = 0; i < symbols.length(); i++) {
char c = symbols.charAt(i);
if (symbolLookup[(int) c] == -1) {
symbolLookup[(int) c] = i;
} else {
throw new IllegalArgumentException("duplicate symbol:" + c);
}
}
}
private static String makeAlphaNumericString(boolean caseSensitive) {
StringBuilder sb = new StringBuilder(255);
int caseDiff = 'a' - 'A';
for (int i = 'A'; i <= 'Z'; i++) {
sb.append((char) i);
if (caseSensitive) sb.append((char) (i + caseDiff));
}
for (int i = '0'; i <= '9'; i++) {
sb.append((char) i);
}
return sb.toString();
}
public String encodeNumber(long decNum) {
return encodeNumber(decNum, 0);
}
public String encodeNumber(long decNum, int minLen) {
StringBuilder result = new StringBuilder(20);
long num = decNum;
long mod = 0;
int base = symbols.length();
do {
mod = num % base;
result.append(symbols.charAt((int) mod));
num = Math.round(Math.floor((num-mod) / base));
} while (num > 0);
if (result.length() < minLen) {
for (int i = result.length(); i < minLen; i++) {
result.append(symbols.charAt(0));
}
}
return result.toString();
}
public long decodeNumber(String encNum) {
if (encNum == null) return 0;
if (!isCaseSensitive) encNum = encNum.toUpperCase();
long result = 0;
int base = symbols.length();
long multiplier = 1;
for (int i = 0; i < encNum.length(); i++) {
char c = encNum.charAt(i);
int pos = symbolLookup[(int) c];
if (pos == -1) {
String debugValue = encNum.substring(0, i) + "[" + c + "]";
if (encNum.length()-1 > i) {
debugValue += encNum.substring(i + 1);
}
throw new IllegalArgumentException(
"invalid symbol '" + c + "' at position "
+ (i+1) + ": " + debugValue);
} else {
result += pos * multiplier;
multiplier = multiplier * base;
}
}
return result;
}
#Override
public String toString() {
return symbols;
}
}
The test:
public class EncodeUtilTest {
#Test
public void testRoundTrip() throws Exception {
//for some reason, numbers larger than this range will not be decoded correctly
//maybe some bug in JVM with arithmetic with long values?
//tried also BigDecimal, didn't make any difference
//anyway, it is highly improbable that we ever need such large numbers
long value = 288230376151711743L;
test(value, new EncodeUtil());
test(value, new EncodeUtil(false));
test(value, new EncodeUtil(true, "1234567890qwertyuiopasdfghjklzxcvbnm"));
}
#Test
public void testRoundTripMax() throws Exception {
//this will fail, see above
test(Long.MAX_VALUE, new EncodeUtil());
}
#Test
public void testRoundTripGettingCloserToMax() throws Exception {
//here we test different values, getting closer to Long.MAX_VALUE
//this will fail, see above
EncodeUtil util = new EncodeUtil();
for (long i = 1000; i > 0; i--) {
System.out.println(i);
test(Long.MAX_VALUE / i, util);
}
}
private void test(long number, EncodeUtil util) throws Exception {
String encoded = util.encodeNumber(number);
long result = util.decodeNumber(encoded);
long diff = number - result;
//System.out.println(number + " = " + encoded + " diff " + diff);
assertEquals("original=" + number + ", result=" + result + ", encoded=" + encoded, 0, diff);
}
}
Any ideas why things start failing when the values get large? I also tried BigInteger, but it did not seem to make a difference.
You're using floating point maths in your encodeNumber method, which makes your code rely on the precision of the double type.
Replacing
num = Math.round(Math.floor((num-mod) / base));
with
num = (num - mod) / base;
Makes the tests pass. Actually
num = num / base;
Should work just as well (thought experiment: what is 19 / 10 when / is integer division?).
You have a conversion to double in your code, which could be generating strange results for large values.
num = Math.round(Math.floor((num-mod) / base));
that would be my first port of call.

Categories

Resources