kiprobinson / bigfraction Goto Github PK
View Code? Open in Web Editor NEWJava class that represents a fraction as a ratio of two BigIntegers, reduced to lowest terms.
License: Other
Java class that represents a fraction as a ratio of two BigIntegers, reduced to lowest terms.
License: Other
It says on the main github page that the java7 branch compiles in Java 7 and also java 5-6. This is in fact not the case, as the Long.compare() method used by the LongFraction class does not exist before java 7. I often use the BigFraction class in Java 6, so it would be nice if it worked in that version.
I also noticed a lot of new features added. I noticed the roundToDenominator() method, however you can extend this to any value using round(x,m)=round(x/m)*m
. m here is the increment to which you are rounding, and the single argument round() function rounds to integral value.
Another question I have is regarding the ()
syntax for repeating digits. Even though the parsers never handle it this way, I think a syntax like 0.1(6) is confusing, as it could be interpreted as 0.16... or 0.6. Is there a better syntax? Or do you think ()
is the best option?
Hi,
For the double representation issue, as it's specified in the javadoc,
System.out.println(BigFraction.valueOf(1.1))
will return 2476979795053773/2251799813685248
But if the following snippet is used :
System.out.println(BigFraction.valueOf(1.1+""));
It will return : 11/10
So is there a reason not to convert a Number in a String before initializing the fraction ? It seems to me that it's solving this problem.
By the way, thanks for the amazing job you've done here !
Laurent
Just a quick little feature I thought I'd suggest. Much like BigInteger has a toString(radix) option, you could add a radix argument to your toString methods. That way fractions could be expressed in arbitrary bases. 57/11, for example, could be expressed in binary as 111001/1011, for example.
Implementing this for the default toString() methods should be trivial. toMixedString() shouldn't be that difficult either. toDecimalString() might be a little harder, but I'm sure it can be done.
I took a look again at your methods recently, and besides the modulo optimizations, I noticed a few things:
integralQuotient
method is identical to the divideToIntegralValue
method. Why have both?gcf(a/b,c/d)=gcf(a,c)/lcm(b,d)
pow
method, it returns reduced fractions, even though if a fraction a/b is already in simplest form, (a/b)^n is also in simplest form and needs no reduction. Why does the pow
method need to reduce?Keep up the good work!
Hello,
I love your fraction class. It's probably the most detailed class out there, with the most functions of anything I could find on the Internet. I wasn't sure how else to contact you, and since I didn't want to throw a plethora of GitHub issues at you, I decided to use this issue. Anyway, I hope I am communicating this right and I hope to hear from you soon. Here are the questions:
Questions:
Feature Requests:
import com.github.kiprobinson.util.BigFraction;
public class BigFractionRemainderMethods{
//calculates the quotient of x and y, rounded down to the nearest integer, and returns it as a BigFraction. Mathematically equal to floor(x/y)
private static BigFraction floorDivision(BigFraction x,BigFraction y) {
BigFraction r = BigFraction.valueOf((x.divide(y)).getIntegerPart());
if ((x.signum()^y.signum())<0&&((r.multiply(y)).compareTo(x)!=0)){
r=r.subtract(BigFraction.ONE);
}
return r;
}
//calculates the proper remainder of x and y, mathematically equal to x-floor(x/y)*y
public static BigFraction properMod(BigFraction x,BigFraction y){
return x.subtract(floorDivision(x,y).multiply(y));
}
//calculates the proper Euclidian remainder of x and y, mathematically equal to x-floor(x/abs(y))*abs(y)
public static BigFraction properEuclidianMod(BigFraction x,BigFraction y){
return x.subtract(floorDivision(x,y.abs()).multiply(y.abs()));
}
//calculates the remainder of x and y consistent with the % operator. Equal to x%y , mathematically equal to x-truncate(x/y)*y
public static BigFraction remainder(BigFraction x,BigFraction y){
return x.subtract(BigFraction.valueOf((x.divide(y)).getIntegerPart()).multiply(y));
}
//returns a BigFraction[] with the first element equal to floorDivision(x,y) and the second element equal to properMod(x,y)
public static BigFraction[] properDivMod(BigFraction x,BigFraction y){
BigFraction[] r=new BigFraction[2];
r[0]=floorDivision(x,y);
r[1]= x.subtract(r[0].multiply(y));
return r;
}
//returns a BigFraction[] with the first element equal to floorDivision(x,y.abs()).multiply(y.signum()) and the second element equal to properEuclidianMod(x,y)
public static BigFraction[] properEuclidianDivMod(BigFraction x,BigFraction y){
BigFraction[] r=new BigFraction[2];
r[0]=floorDivision(x,y.abs()).multiply(y.signum());
r[1]= x.subtract(r[0].multiply(y));
return r;
}
//returns a BigFraction[] with the first element equal to (x.divide(y)).getIntegerPart() and the second element equal to remainder(x,y)
public static BigFraction[] divideAndRemainder(BigFraction x,BigFraction y){
BigFraction[] r=new BigFraction[2];
r[0]=BigFraction.valueOf((x.divide(y)).getIntegerPart());
r[1]= x.subtract(r[0].multiply(y));
return r;
}
public static void main(String[] args){
}
}
Bugs:
9 errors and 73 warnings found:
--------------
*** Errors ***
--------------
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 98]
Error: error: reference not found
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 125]
Error: error: reference not found
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 259]
Error: error: reference not found
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 279]
Error: error: reference not found
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 311]
Error: error: reference not found
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 311]
Error: error: bad HTML entity
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 311]
Error: error: bad HTML entity
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 311]
Error: error: malformed HTML
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 331]
Error: error: reference not found
--------------
** Warnings **
--------------
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 63]
Warning: warning: no @param for n
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 63]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 100]
Warning: warning: no @param for numerator
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 100]
Warning: warning: no @param for denominator
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 100]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 127]
Warning: warning: no @param for s
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 127]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 145]
Warning: warning: no @param for n
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 145]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 170]
Warning: warning: no @param for a
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 170]
Warning: warning: no @param for b
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 170]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 178]
Warning: warning: no @param for n
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 178]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 203]
Warning: warning: no @param for n
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 203]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 228]
Warning: warning: no @param for a
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 228]
Warning: warning: no @param for b
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 228]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 236]
Warning: warning: no @param for n
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 236]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 251]
Warning: warning: no @param for a
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 251]
Warning: warning: no @param for b
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 251]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 261]
Warning: warning: no @param for n
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 261]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 281]
Warning: warning: no @param for n
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 281]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 299]
Warning: warning: no @param for a
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 299]
Warning: warning: no @param for b
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 299]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 313]
Warning: warning: no @param for exponent
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 313]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 333]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 345]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 354]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 362]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 370]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 380]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 391]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 403]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 414]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 426]
Warning: warning: no @param for roundingMode
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 426]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 532]
Warning: warning: no @param for newDenominator
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 547]
Warning: warning: no @param for newDenominator
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 547]
Warning: warning: no @param for roundingMode
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 578]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 596]
Warning: warning: no @param for numDecimalDigits
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 596]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 610]
Warning: warning: no description for @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 682]
Warning: warning: no @param for n
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 682]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 730]
Warning: warning: no @param for maxDenominator
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 730]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 744]
Warning: warning: no @param for maxDenominator
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 744]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 755]
Warning: warning: no @param for maxDenominator
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 755]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 848]
Warning: warning: no @param for n
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 848]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 859]
Warning: warning: no @param for n
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 859]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 872]
Warning: warning: no @param for f
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 872]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 888]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 955]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 986]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 1013]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 1040]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 1067]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 1095]
Warning: warning: no @return
File: /Users/elygolden/Documents/Local Docs/Acquired Codestuffs/Packages/BigFraction-master/src/com/github/kiprobinson/util/BigFraction.java [line: 1131]
Warning: warning: no @return
I checked the IEEE754 standard and found that there's a special case you didn't concerned.
That is, when the exponent bits are zeros and the mantissa is non-zero, the standard treats exponent as, for double as an example, -1022(instead of -1023, just plus 1), and the number is (-1)^sign * 2^exponent * 0.mantissa(instead of 1.mantissa).
So your code made different numbers. Hope you can fix it soon.
An example:
System.out.println(BigFraction.valueOf(Double.longBitsToDouble(0x0000000000000001L)).doubleValue());
System.out.println(Double.longBitsToDouble(0x0000000000000001L));
The first outputs:
1.112536929253601E-308
but the second outputs:
4.9E-324
Here is a tool can do the conversion: http://www.binaryconvert.com/convert_double.html
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.