string - java.lang.StackOverflowError exception for large inputs -


basically, i'm writing program simple division manually want decimal place upto 10^6 places. program works inputs <3000, when go higher, shows: exception in thread "main" java.lang.stackoverflowerror

here's code:

{ .... .... int n=100000;//nth place after decimal point string res=obj.compute(n,103993.0,33102.0,ans); //division of 103993.0 33102.0 system.out.println(res); }  public string compute (int n, double a, double b, string ans){         int x1=(int)a/(int)b;         double x2=a-x1*b;         double x3=x2*10;         int c=0;         if (n==0||n<0)             return ("3."+ans.substring(1));         else if (x3>b){             ans+=""+x1;             c=1;         }         else if(x3*10>b){             ans+=x1+"0";             c=10;         }         else if(x3*100>b){             ans+=x1+"00";             c=100;         }         else if(x3*1000>b){             ans+=x1+"000";             c=1000;         }         else if(x3*10000>b){             ans+=x1+"0000";             c=10000;         }         return compute(n-string.valueof(c).length(),x3*c,b,ans);     } 

i'm not hard-core programmer of java. need in tackling situation. read posts increasing stack size, didn't understand method.

using recursivity kind of computation idea, every sub-call make, stores pointers , other info in stack, filling it. don't know depths of vm, think jvm's max heap or stack size depends on how contiguous free memory can reserved, problem might solved using the -xssparameter, changes size of stack (e.g. java -xss8m yourclass). if still doesn't work or cannot enought memory, try 64-bit jvm.

if doesn't workk, contrary usual practice try program without recursivity.

i hope helps!


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -