Advertisement
Guest User

big number up to 1e1e308

a guest
Nov 13th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.99 KB | None | 0 0
  1. package com.antoine.mathematician.oddlittlegame;
  2.  
  3. class Infinite {
  4. private double mantissa;
  5. private double exponent;
  6. private double mantissaLog;
  7. private static final double Precision = 6;
  8. double deltaLogMantissa;
  9. private boolean isMantissaDead;
  10. private static Infinite TInf;
  11. private String myVal="";
  12. private String mantissaStr="";
  13. private String exponentStr="";
  14. double ExponentHeight=0;
  15. static boolean isMirrored=false;
  16. private Infinite(double val1){
  17. if (val1<=1) {
  18. exponent=0;
  19. }
  20. else {
  21. exponent=Math.round(Math.log10(val1));
  22. }
  23. mantissa=val1 / Math.pow(10,exponent);
  24. shiftMantissa();
  25. }
  26. private Infinite(double Mantissa, double Exponent){
  27. mantissa=Mantissa;
  28. if (mantissa==0){
  29. exponent=0;
  30. }else{
  31. exponent=Exponent;
  32. }
  33. shiftMantissa();
  34. }
  35.  
  36. void add(Infinite val1)
  37. {
  38. if (Math.abs(exponent-val1.exponent)>Precision)
  39. {
  40. if (exponent<val1.exponent)
  41. {
  42. this.mantissa = val1.mantissa;
  43. this.exponent = val1.exponent;
  44. }
  45. }
  46. else
  47. {
  48. this.mantissa += val1.mantissa / Math.pow(10,this.exponent-val1.exponent);
  49.  
  50. }
  51. shiftMantissa();
  52. }
  53. void sub(Infinite val1)
  54. {
  55. boolean res;
  56. Infinite infinite= new Infinite(mantissa,exponent);
  57. this.mantissa -= val1.mantissa / Math.pow(10,this.exponent-val1.exponent);
  58. if (mantissa<0) {
  59. mantissa=0;
  60. res=infinite.isGreater(val1);
  61. if (res){
  62. res=infinite.isGreater(val1);
  63. }
  64. }
  65. shiftMantissa();
  66. }
  67.  
  68. void add(Infinite val1,Infinite Multiplier)
  69. {
  70. TInf=new Infinite(val1.mantissa,val1.exponent);
  71. TInf.multiply(Multiplier);
  72. if (Math.abs(TInf.exponent+ TInf.mantissaLog-exponent-mantissaLog)>=Precision)
  73. {
  74. if (exponent+mantissaLog<=TInf.exponent+TInf.mantissaLog)
  75. {
  76. this.mantissa = TInf.mantissa;
  77. this.exponent = TInf.exponent;
  78. }
  79. else{
  80.  
  81. }
  82. }
  83. else
  84. {
  85. this.mantissa += (TInf.mantissa)/ Math.pow(10,this.exponent-TInf.exponent);
  86.  
  87. }
  88. shiftMantissa();
  89. }
  90.  
  91. private boolean isGreater(Infinite val1)
  92. {
  93. if (MainActivity.ReachedInfinity){
  94. return true;
  95. }
  96. if (Math.abs(exponent+mantissaLog-val1.exponent-val1.mantissaLog)>=Precision)
  97. {
  98. return (exponent+Math.log10(mantissa))>=(val1.exponent+Math.log10(val1.mantissa));
  99. }
  100. else
  101. {
  102. return this.mantissa >= val1.mantissa / Math.pow(10,this.exponent-val1.exponent);
  103. }
  104. }
  105. void multiply(double val1)
  106. {
  107. if (val1==0){
  108. this.mantissa=0;
  109. this.exponent=0;
  110. }else {
  111. Infinite val2 = new Infinite(val1);
  112. this.multiply(val2);
  113. }
  114. }
  115. Infinite multiply(Infinite val1,boolean newInf)
  116. {
  117. return new Infinite(this.mantissa * val1.mantissa,this.exponent + val1.exponent);
  118. }
  119. private void multiply(Infinite val1)
  120. {
  121. this.mantissa *= val1.mantissa;
  122. this.exponent += val1.exponent;
  123. shiftMantissa();
  124. }
  125.  
  126. private void power(Infinite val1){
  127.  
  128. double tval=val1.mantissa*Math.pow(10,val1.exponent);
  129. if (tval==0){
  130. mantissa=1;
  131. exponent=0;
  132. shiftMantissa();
  133. }else if (tval==1){
  134.  
  135. }else {
  136. if (!Double.isInfinite(tval)) {
  137. if (this.mantissa==1){
  138.  
  139. }else{
  140. this.mantissa=Math.pow(this.mantissa/Math.pow(10,this.mantissaLog),tval);
  141. }
  142.  
  143. this.exponent=(this.exponent+this.mantissaLog)*tval;
  144. if (exponent<1e17){
  145. this.exponent=Math.round(this.exponent);
  146. }
  147. shiftMantissa();
  148. }else
  149. {
  150.  
  151. }
  152.  
  153. }
  154.  
  155.  
  156. }
  157. Infinite power(Infinite val1,boolean newInf){
  158.  
  159. double tval=val1.mantissa*Math.pow(10,val1.exponent);
  160. Infinite tVal = new Infinite(mantissa,exponent);
  161. if (tval==0){
  162. tVal.mantissa=1;
  163. tVal.exponent=0;
  164. tVal.shiftMantissa();
  165. return tVal;
  166. }else if (tval==1){
  167. return tVal;
  168. }else {
  169. if (!Double.isInfinite(tval)) {
  170. if (this.mantissa==1){
  171. tVal.mantissa=1;
  172. }else{
  173. tVal.mantissa=Math.pow(this.mantissa/Math.pow(10,this.mantissaLog),tval);
  174. }
  175.  
  176. tVal.exponent=(this.exponent+this.mantissaLog)*tval;
  177. tVal.exponent=Math.round(tVal.exponent);
  178. tVal.shiftMantissa();
  179. return tVal;
  180. }else
  181. {
  182.  
  183. }
  184. return tVal;
  185. }
  186.  
  187.  
  188. }
  189. void multiply(Infinite val1,Infinite PowerPlier)
  190. {
  191. TInf=new Infinite(val1.mantissa,val1.exponent);
  192. TInf.power(PowerPlier);
  193. this.mantissa *= TInf.mantissa;
  194. this.exponent += TInf.exponent;
  195. shiftMantissa();
  196. }
  197. void divide(double val1)
  198. {
  199. if (val1==0){
  200. this.mantissa=0;
  201. this.exponent=0;
  202. }else {
  203. Infinite val2 = new Infinite(val1);
  204. this.divide(val2);
  205. }
  206. }
  207. private void divide(Infinite val1)
  208. {
  209. if (val1.mantissa!=0){
  210. this.mantissa /= val1.mantissa;
  211. this.exponent -= val1.exponent;
  212. shiftMantissa();
  213. }
  214. }
  215. Long log_X(double val1){
  216. double tVal;
  217. tVal=Math.log10(val1);
  218. if (mantissa==0) {
  219. return 1L;
  220. }
  221. long tval = (long)(Math.floor((this.exponent+Math.log10(mantissa))/tVal))+2;
  222. if (tval>1000) {
  223. return 1000L;
  224. }
  225. return tval;
  226. }
  227. Long log_X(Infinite val1,Infinite divider){
  228. Infinite infinite;
  229. double tVal;
  230. tVal=Math.log10(val1.mantissa)+val1.exponent;
  231. if (mantissa==0) {
  232. return 1L;
  233. }
  234. infinite = new Infinite(mantissa,exponent);
  235. infinite.divide(divider);
  236. long tval = (long)(Math.floor((infinite.exponent+Math.log10(infinite.mantissa))/tVal))+2;
  237. if (tval>1000) {
  238. return 1000L;
  239. }
  240. return tval;
  241. }
  242.  
  243. void add1(double prestigeLevel){
  244. if (prestigeLevel==0){
  245. add1();
  246. }else {
  247. this.mantissa += 0.0001 / Math.pow(10,this.exponent);
  248. }
  249. shiftMantissa();
  250. }
  251. private void add1()
  252. {
  253. this.mantissa += 1 / Math.pow(10,this.exponent);
  254. shiftMantissa();
  255.  
  256. }
  257. void sub1()
  258. {
  259. this.mantissa -= 1 / Math.pow(10,this.exponent);
  260. shiftMantissa();
  261.  
  262. }
  263. private void shiftMantissa(){
  264. if (this.mantissa==0){
  265. mantissaLog=0;
  266. exponent=0;
  267. }else{
  268. mantissaLog= Math.floor(Math.log10(this.mantissa));
  269. }
  270.  
  271. if (mantissaLog>Precision || mantissaLog<-Precision) {
  272. this.mantissa/=Math.pow(10,mantissaLog);
  273. this.exponent+=mantissaLog;
  274. mantissaLog=0;
  275. }
  276. if (!isMantissaDead){
  277. isMantissaDead =(exponent+1)==exponent;
  278. }
  279.  
  280.  
  281. }
  282.  
  283. String ItoString() {
  284. return ItoString(0,false,false);
  285. }
  286. double toDouble(){
  287. return mantissa*Math.pow(10,exponent);
  288. }
  289. String ItoString(int prestigeLvl) {
  290. return ItoString(prestigeLvl,false,false);
  291. }
  292. String ItoString(int prestigeLvl,boolean SingleMantissa) {
  293. return ItoString(prestigeLvl,false,SingleMantissa);
  294. }
  295. private String ItoString(int prestigeLvl, boolean longView, boolean SmallMantissa){
  296. if (this.mantissa<=0){
  297. mantissaLog=0;
  298. }
  299. else{
  300. mantissaLog= Math.floor(Math.log10(this.mantissa));
  301. }
  302. //deltaLogMantissa=Math.floor(mantissaLog-Precision);
  303. myVal="";
  304. if (isMantissaDead && prestigeLvl==0){
  305. mantissaStr="1";
  306. //if (!Achievements.BrokeMantissa){
  307. // Achievements.UnlockBreakMantissa(MainActivity.myContext);
  308. //}
  309. }
  310. else if (exponent+mantissaLog<8 ){
  311. if (prestigeLvl==0){
  312. mantissaStr=String.valueOf(Math.round(mantissa*Math.pow(10,exponent)));
  313. }else {
  314. mantissaStr=String.valueOf(roundMantissa(mantissa*Math.pow(10,exponent)));
  315. }
  316.  
  317. }
  318. else{
  319. mantissaStr=String.valueOf(roundMantissa(mantissa/Math.pow(10,mantissaLog)));
  320. while (mantissaStr.length()<8){
  321. mantissaStr+="0";
  322. }
  323. }
  324. if (SmallMantissa){
  325. int occurrences = 0;
  326. for(char c : mantissaStr.toCharArray()){
  327. if(c == '.'){
  328. occurrences++;
  329. }
  330. //if (occurrences=1)
  331. }
  332. if (prestigeLvl==0) {
  333. if (occurrences==1){
  334. if (mantissaStr.length()!=1){
  335. mantissaStr = mantissaStr.substring(0,mantissaStr.indexOf(".")+3);
  336. }
  337. }
  338. }
  339.  
  340.  
  341.  
  342.  
  343. }
  344. if (Double.isInfinite(exponent)){
  345. myVal="Infinity";
  346. }
  347. else if (Math.log10(exponent)>15){
  348. exponentStr =String.valueOf(roundExponent(Math.log10(exponent)));
  349. while (exponentStr.length()<10){
  350. exponentStr+="0";
  351. }
  352. if (exponentStr.length()>10){
  353. exponentStr=exponentStr.substring(0,10);
  354. }
  355. myVal = mantissaStr + "e1e" + exponentStr;
  356. }
  357. else if(Math.log10(exponent)>7) {
  358. if (longView) {
  359. exponentStr = String.valueOf(mantissaLog+exponent).replace(".","");
  360. exponentStr=exponentStr.substring(0,exponentStr.indexOf("E"));
  361. myVal = mantissaStr + "e1e" +exponentStr;
  362. }
  363. else{
  364. exponentStr =String.valueOf(roundExponent(Math.log10(exponent)));
  365. while (exponentStr.length()<10){
  366. exponentStr+="0";
  367. }
  368. if (exponentStr.length()>10){
  369. exponentStr=exponentStr.substring(0,10);
  370. }
  371. myVal = mantissaStr + "e1e" +exponentStr;
  372. }
  373.  
  374.  
  375. }
  376. else if (exponent+mantissaLog<8){
  377. if (prestigeLvl==0){
  378. myVal = mantissaStr.replace(".0","");
  379. }
  380. else {
  381. myVal =mantissaStr;
  382. }
  383. }
  384. else
  385. {
  386. myVal = mantissaStr + "e" + String.valueOf(mantissaLog+exponent).replace(".0","");
  387. }
  388.  
  389. //myVal=mantissaStr.substring(0,1) +'.'+ mantissaStr.substring(1) + " E" + String.valueOf(exponent+deltaLogMantissa);
  390. if (isMirrored){
  391. return reverse(myVal);
  392. }else {
  393. return myVal;
  394. }
  395.  
  396.  
  397.  
  398. }
  399.  
  400.  
  401. private double roundMantissa(double val1){
  402. return Math.round(val1*Math.pow(10,Precision))/Math.pow(10,Precision);
  403. }
  404. private double roundExponent(double val1){
  405. return Math.round(val1*Math.pow(10,8))/Math.pow(10,8);
  406. }
  407. private static String reverse(String string)
  408. {
  409. StringBuilder input1 = new StringBuilder();
  410.  
  411. // append a string into StringBuilder input1
  412. input1.append(string);
  413.  
  414. // reverse StringBuilder input1
  415. input1 = input1.reverse();
  416.  
  417. // print reversed String
  418. return input1.toString();
  419. }
  420. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement