Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Tolls

Thread: use "long" variable for math.pow

  1. #1
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default use "long" variable for math.pow

    Hi everyone,

    to solve a little problem, I need to make the exponentiation of veeery big numbers...apparently, out of the range of the double variable, trying to use a long variable, the compilator turn sick, and the method Math.pow expect a double variable...here's the error:
    possible loss of precision
    required: long
    found: double
    (I know, it looks like it's the opposite, but the error say so..and if I turn the long variable to double, it works)

    is there a way to use long variable with math.pow? Or another way to make exponentiation?

    here's the code for clarification...

    Java Code:
     */
    package prova2;
    
    
    public class Prova2{
        public static void main(String[] args){
     int i=1;
    
     int a = 0;
     long s;
     
     for (int f = 1; f<1001;f++){
        
    
         s = Math.pow(i, i);
         a += s;
         i++;
         
     }
    
         System.out.println(a);
    
        }
    }
    (the point of the code is print the sum of 1^1 + 2^2 + 3^3.....1000^1000)
    Last edited by DarrylBurke; 08-20-2012 at 07:29 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: use "long" variable for math.pow

    Or another way to make exponentiation?
    See BigInteger (Java Platform SE 6)

  3. #3
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: use "long" variable for math.pow

    ok, I definitely understood nothing of the page you linked me...xD could you explain please?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: use "long" variable for math.pow

    The linked page is for the BigInteger class in the Java API. You use this class to handle large integer values. Oh look it even has its own pow method.

    What exactly don't you understand?

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: use "long" variable for math.pow

    A BigInteger is just that, a BigInteger - no real restrictions in bytes like the java primitives have. Use the operations of the class like so:

    Java Code:
    		BigInteger bi = BigInteger.valueOf(2L);
    		BigInteger four = bi.pow(2);
    		BigInteger five = four.add(BigInteger.valueOf(1L));
    You should however, verify your assumptions in your original post are correct (in the code you posted above, you're tally is an int (eg the variable a), not a long nor a double)

  6. #6
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: use "long" variable for math.pow

    I can't figure out how to use it, I tryed to type
    Java Code:
    BigInteger bi = BigInteger.pow(i)
    as said in the tutorial, but it give me this error:
    non static method pow(int) cannot be referenced from a static context.

    ...and I didn't even understood, if the (int) is the exponent, where does it take the base?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default Re: use "long" variable for math.pow

    Other way around.
    The method is an object method, not a static method.
    You need a BigInteger object to actually use it.
    Fubarable likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: use "long" variable for math.pow

    Tolls: thanks for the correction. Incorrect answer deleted.

  9. #9
    Chri is offline Member
    Join Date
    Aug 2012
    Location
    Switzerland
    Posts
    49
    Rep Power
    0

    Default Re: use "long" variable for math.pow

    DID IT!

    thank to all :D

    here's my solution

    Java Code:
    package prova2;
    
    
    import java.math.BigInteger;
    public class Prova2{
        public static void main(String[] args){
     BigInteger bi = BigInteger.valueOf(0L);
     BigInteger a = BigInteger.valueOf(0L);
     BigInteger somma = BigInteger.valueOf(0L);
     BigInteger pot = BigInteger.valueOf(0L);
    
     
    
    for (int  i= 1; i<1001 ;i++){
          somma = somma.add(pot); 
     
    
     bi = BigInteger.valueOf(i);
            pot = bi.pow(i); 
        
    
    
       
                 
     }
    System.out.println(somma);
    
    
        }
         
    }
    and the result xD

    36819914469517709537501122764679556779368062293465 45837609881002349107477161943814286590995278459458 69942643191290894720342979906407679647259860434238 46803832604080969103761537037623771364851006311573 29514617742467055842668657596018158436664428322845 56880313114548151539190975398485496645576513465858 58271233640116622195618817344953167410268890832176 46630203066997704086253407660915950227913793680983 69306375602813856646358773751558775213460225796579 84658333400734935862434233933298133457123788880928 31033487602613601759508156091794640268710052436521 09980863552142014242903434068560936573231079342194 03186441391810123815105650926739351576039284247250 13915940734630015218438110737670217110263075046957 33467897821866906648469828346607412967395801797791 68360983472243224195284535256468186824036956956619 28255553235580780619975276899838488633747867893315 81565252059172614339424600986143259233167583371070 36262555453185205416611714885822950858158961433759 44632775543805183809213012188363271022314073322011 09740102580216469298331766920619646083790732807627 36061442808517156500628972850868896422679964719258 29240585895307506745783853655618785595896857562256 92348914746922810913915619834754117648358035814128 67029415856566994208773628639094224154722601500447 13306301130720427042889050421426281937719185945743 02202147201188486345913190833752307476966010547423 92887106311878302603638131903905200825207205793366 67129189462333127936970940742241878720459709764443 09242782187738320257490080824330074991698698239561 12581112760786390035522173784669056770734407449414 52666621038398128402163034484769139570723557327166 27098372245223046792919747259113157425824064858331 41540094327821304295463505357404520998451222126424 19035501784168245514125486375900077790825392882477 51653566899882749594405895102587985539527709493510 04954644542726561747839910718823868177121590423411 93922474897510790859480559450988056179637229284695 54263782217625160428008228845552540344494860195267 11518709222776619575390721112664615014061474423397 47652734756199643118528586141678196683401247304877 10162006793529985758820653677274379563313495454526 63271872348233949482575982107640169431604345651211 79379354564635214630211977266949835589291323575761 88594977516630734212863869456164205525536767311298 13718251149464946366307375921921305682356166777609 37394257428839307126099621634640880388265691320321 60692637206183085942987973684584276491784843115472 07790040169259569411927355351102599126544603936628 89217435813332000837171052411715046068835434188620 24047552177055263424469501298905901938158245938633 69410502481516667981368915666834119771347509438990 48871267944689018938504750500112052257424555556257 50560213230387910337983950333245020653238989115507 01388295627776388079568721085719649389314265671310 59662754221446059880589396006036042269214014020965 19294250488670297983396353279460453142375542267881 98919748178978067895509376319365860369089847482697 69065444739780174557203679299817960230417858526267 97271283465789498383642350667978127819110846700

    how do i mark the thread as solved?

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: use "long" variable for math.pow

    I think you can edit your original post, use the "Advanced" view and choose "Solved" as the prefix.

    The forum software is a bit weird in this respect (there was discussion of that recently) and my phone won't let me try this for you.

    ---

    The cartesian in me can't resist asking: how do you know it's true? Is there some tricky property of sigma(i pow i) that you can use to verify the result?

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: use "long" variable for math.pow

    Marked as SOLVED

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. "Best" implementation of math operator methods?
    By rgrant222 in forum New To Java
    Replies: 2
    Last Post: 09-01-2010, 07:47 AM
  2. Math.random() without a "0"
    By FlyNn in forum New To Java
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  3. Error "can not find symbol variable"
    By FullMetalHollow in forum New To Java
    Replies: 5
    Last Post: 10-04-2009, 09:51 PM
  4. Replies: 2
    Last Post: 11-30-2008, 03:24 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •