Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.antoine.mathematician.oddlittlegame;
- class Infinite {
- private double mantissa;
- private double exponent;
- private double mantissaLog;
- private static final double Precision = 6;
- double deltaLogMantissa;
- private boolean isMantissaDead;
- private static Infinite TInf;
- private String myVal="";
- private String mantissaStr="";
- private String exponentStr="";
- double ExponentHeight=0;
- static boolean isMirrored=false;
- private Infinite(double val1){
- if (val1<=1) {
- exponent=0;
- }
- else {
- exponent=Math.round(Math.log10(val1));
- }
- mantissa=val1 / Math.pow(10,exponent);
- shiftMantissa();
- }
- private Infinite(double Mantissa, double Exponent){
- mantissa=Mantissa;
- if (mantissa==0){
- exponent=0;
- }else{
- exponent=Exponent;
- }
- shiftMantissa();
- }
- void add(Infinite val1)
- {
- if (Math.abs(exponent-val1.exponent)>Precision)
- {
- if (exponent<val1.exponent)
- {
- this.mantissa = val1.mantissa;
- this.exponent = val1.exponent;
- }
- }
- else
- {
- this.mantissa += val1.mantissa / Math.pow(10,this.exponent-val1.exponent);
- }
- shiftMantissa();
- }
- void sub(Infinite val1)
- {
- boolean res;
- Infinite infinite= new Infinite(mantissa,exponent);
- this.mantissa -= val1.mantissa / Math.pow(10,this.exponent-val1.exponent);
- if (mantissa<0) {
- mantissa=0;
- res=infinite.isGreater(val1);
- if (res){
- res=infinite.isGreater(val1);
- }
- }
- shiftMantissa();
- }
- void add(Infinite val1,Infinite Multiplier)
- {
- TInf=new Infinite(val1.mantissa,val1.exponent);
- TInf.multiply(Multiplier);
- if (Math.abs(TInf.exponent+ TInf.mantissaLog-exponent-mantissaLog)>=Precision)
- {
- if (exponent+mantissaLog<=TInf.exponent+TInf.mantissaLog)
- {
- this.mantissa = TInf.mantissa;
- this.exponent = TInf.exponent;
- }
- else{
- }
- }
- else
- {
- this.mantissa += (TInf.mantissa)/ Math.pow(10,this.exponent-TInf.exponent);
- }
- shiftMantissa();
- }
- private boolean isGreater(Infinite val1)
- {
- if (MainActivity.ReachedInfinity){
- return true;
- }
- if (Math.abs(exponent+mantissaLog-val1.exponent-val1.mantissaLog)>=Precision)
- {
- return (exponent+Math.log10(mantissa))>=(val1.exponent+Math.log10(val1.mantissa));
- }
- else
- {
- return this.mantissa >= val1.mantissa / Math.pow(10,this.exponent-val1.exponent);
- }
- }
- void multiply(double val1)
- {
- if (val1==0){
- this.mantissa=0;
- this.exponent=0;
- }else {
- Infinite val2 = new Infinite(val1);
- this.multiply(val2);
- }
- }
- Infinite multiply(Infinite val1,boolean newInf)
- {
- return new Infinite(this.mantissa * val1.mantissa,this.exponent + val1.exponent);
- }
- private void multiply(Infinite val1)
- {
- this.mantissa *= val1.mantissa;
- this.exponent += val1.exponent;
- shiftMantissa();
- }
- private void power(Infinite val1){
- double tval=val1.mantissa*Math.pow(10,val1.exponent);
- if (tval==0){
- mantissa=1;
- exponent=0;
- shiftMantissa();
- }else if (tval==1){
- }else {
- if (!Double.isInfinite(tval)) {
- if (this.mantissa==1){
- }else{
- this.mantissa=Math.pow(this.mantissa/Math.pow(10,this.mantissaLog),tval);
- }
- this.exponent=(this.exponent+this.mantissaLog)*tval;
- if (exponent<1e17){
- this.exponent=Math.round(this.exponent);
- }
- shiftMantissa();
- }else
- {
- }
- }
- }
- Infinite power(Infinite val1,boolean newInf){
- double tval=val1.mantissa*Math.pow(10,val1.exponent);
- Infinite tVal = new Infinite(mantissa,exponent);
- if (tval==0){
- tVal.mantissa=1;
- tVal.exponent=0;
- tVal.shiftMantissa();
- return tVal;
- }else if (tval==1){
- return tVal;
- }else {
- if (!Double.isInfinite(tval)) {
- if (this.mantissa==1){
- tVal.mantissa=1;
- }else{
- tVal.mantissa=Math.pow(this.mantissa/Math.pow(10,this.mantissaLog),tval);
- }
- tVal.exponent=(this.exponent+this.mantissaLog)*tval;
- tVal.exponent=Math.round(tVal.exponent);
- tVal.shiftMantissa();
- return tVal;
- }else
- {
- }
- return tVal;
- }
- }
- void multiply(Infinite val1,Infinite PowerPlier)
- {
- TInf=new Infinite(val1.mantissa,val1.exponent);
- TInf.power(PowerPlier);
- this.mantissa *= TInf.mantissa;
- this.exponent += TInf.exponent;
- shiftMantissa();
- }
- void divide(double val1)
- {
- if (val1==0){
- this.mantissa=0;
- this.exponent=0;
- }else {
- Infinite val2 = new Infinite(val1);
- this.divide(val2);
- }
- }
- private void divide(Infinite val1)
- {
- if (val1.mantissa!=0){
- this.mantissa /= val1.mantissa;
- this.exponent -= val1.exponent;
- shiftMantissa();
- }
- }
- Long log_X(double val1){
- double tVal;
- tVal=Math.log10(val1);
- if (mantissa==0) {
- return 1L;
- }
- long tval = (long)(Math.floor((this.exponent+Math.log10(mantissa))/tVal))+2;
- if (tval>1000) {
- return 1000L;
- }
- return tval;
- }
- Long log_X(Infinite val1,Infinite divider){
- Infinite infinite;
- double tVal;
- tVal=Math.log10(val1.mantissa)+val1.exponent;
- if (mantissa==0) {
- return 1L;
- }
- infinite = new Infinite(mantissa,exponent);
- infinite.divide(divider);
- long tval = (long)(Math.floor((infinite.exponent+Math.log10(infinite.mantissa))/tVal))+2;
- if (tval>1000) {
- return 1000L;
- }
- return tval;
- }
- void add1(double prestigeLevel){
- if (prestigeLevel==0){
- add1();
- }else {
- this.mantissa += 0.0001 / Math.pow(10,this.exponent);
- }
- shiftMantissa();
- }
- private void add1()
- {
- this.mantissa += 1 / Math.pow(10,this.exponent);
- shiftMantissa();
- }
- void sub1()
- {
- this.mantissa -= 1 / Math.pow(10,this.exponent);
- shiftMantissa();
- }
- private void shiftMantissa(){
- if (this.mantissa==0){
- mantissaLog=0;
- exponent=0;
- }else{
- mantissaLog= Math.floor(Math.log10(this.mantissa));
- }
- if (mantissaLog>Precision || mantissaLog<-Precision) {
- this.mantissa/=Math.pow(10,mantissaLog);
- this.exponent+=mantissaLog;
- mantissaLog=0;
- }
- if (!isMantissaDead){
- isMantissaDead =(exponent+1)==exponent;
- }
- }
- String ItoString() {
- return ItoString(0,false,false);
- }
- double toDouble(){
- return mantissa*Math.pow(10,exponent);
- }
- String ItoString(int prestigeLvl) {
- return ItoString(prestigeLvl,false,false);
- }
- String ItoString(int prestigeLvl,boolean SingleMantissa) {
- return ItoString(prestigeLvl,false,SingleMantissa);
- }
- private String ItoString(int prestigeLvl, boolean longView, boolean SmallMantissa){
- if (this.mantissa<=0){
- mantissaLog=0;
- }
- else{
- mantissaLog= Math.floor(Math.log10(this.mantissa));
- }
- //deltaLogMantissa=Math.floor(mantissaLog-Precision);
- myVal="";
- if (isMantissaDead && prestigeLvl==0){
- mantissaStr="1";
- //if (!Achievements.BrokeMantissa){
- // Achievements.UnlockBreakMantissa(MainActivity.myContext);
- //}
- }
- else if (exponent+mantissaLog<8 ){
- if (prestigeLvl==0){
- mantissaStr=String.valueOf(Math.round(mantissa*Math.pow(10,exponent)));
- }else {
- mantissaStr=String.valueOf(roundMantissa(mantissa*Math.pow(10,exponent)));
- }
- }
- else{
- mantissaStr=String.valueOf(roundMantissa(mantissa/Math.pow(10,mantissaLog)));
- while (mantissaStr.length()<8){
- mantissaStr+="0";
- }
- }
- if (SmallMantissa){
- int occurrences = 0;
- for(char c : mantissaStr.toCharArray()){
- if(c == '.'){
- occurrences++;
- }
- //if (occurrences=1)
- }
- if (prestigeLvl==0) {
- if (occurrences==1){
- if (mantissaStr.length()!=1){
- mantissaStr = mantissaStr.substring(0,mantissaStr.indexOf(".")+3);
- }
- }
- }
- }
- if (Double.isInfinite(exponent)){
- myVal="Infinity";
- }
- else if (Math.log10(exponent)>15){
- exponentStr =String.valueOf(roundExponent(Math.log10(exponent)));
- while (exponentStr.length()<10){
- exponentStr+="0";
- }
- if (exponentStr.length()>10){
- exponentStr=exponentStr.substring(0,10);
- }
- myVal = mantissaStr + "e1e" + exponentStr;
- }
- else if(Math.log10(exponent)>7) {
- if (longView) {
- exponentStr = String.valueOf(mantissaLog+exponent).replace(".","");
- exponentStr=exponentStr.substring(0,exponentStr.indexOf("E"));
- myVal = mantissaStr + "e1e" +exponentStr;
- }
- else{
- exponentStr =String.valueOf(roundExponent(Math.log10(exponent)));
- while (exponentStr.length()<10){
- exponentStr+="0";
- }
- if (exponentStr.length()>10){
- exponentStr=exponentStr.substring(0,10);
- }
- myVal = mantissaStr + "e1e" +exponentStr;
- }
- }
- else if (exponent+mantissaLog<8){
- if (prestigeLvl==0){
- myVal = mantissaStr.replace(".0","");
- }
- else {
- myVal =mantissaStr;
- }
- }
- else
- {
- myVal = mantissaStr + "e" + String.valueOf(mantissaLog+exponent).replace(".0","");
- }
- //myVal=mantissaStr.substring(0,1) +'.'+ mantissaStr.substring(1) + " E" + String.valueOf(exponent+deltaLogMantissa);
- if (isMirrored){
- return reverse(myVal);
- }else {
- return myVal;
- }
- }
- private double roundMantissa(double val1){
- return Math.round(val1*Math.pow(10,Precision))/Math.pow(10,Precision);
- }
- private double roundExponent(double val1){
- return Math.round(val1*Math.pow(10,8))/Math.pow(10,8);
- }
- private static String reverse(String string)
- {
- StringBuilder input1 = new StringBuilder();
- // append a string into StringBuilder input1
- input1.append(string);
- // reverse StringBuilder input1
- input1 = input1.reverse();
- // print reversed String
- return input1.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement