• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Problem with BigInteger

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


When I try to run this code the following error message is coming up.
"The operator + is undefined for the argument type(s) BigInteger,double."
How can I solve this?
Thank you.
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to use methods of the BigInteger class to add something to a BigInteger; "+" won't work.
 
Chaturaka Gunatilaka
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you give me the code of that?I've tried but failed to solve.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chaturaka Gunatilaka wrote:Can you give me the code of that?I've tried but failed to solve.



That's not how it works. If you show your work and TellTheDetails(←click) of what went wrong, people will be happy to guide you toward solving your own problem.
 
Chaturaka Gunatilaka
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Chaturaka Gunatilaka wrote:Can you give me the code of that?I've tried but failed to solve.



That's not how it works. If you show your work and TellTheDetails(←click) of what went wrong, people will be happy to guide you toward solving your own problem.



ok then.

import java.math.BigInteger;
public class AWords{ public static void main(String[]args){
BigInteger sum=null; //BigInteger sum=new BigInteger(null);
for(int i=1;i<=1000;i++){
sum = sum.add(Math.pow(i, i));
System.out.println(sum);
}
}
}
So this is what I've done.but I got an error saying
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method add(BigInteger) in the type BigInteger is not applicable for the arguments (double) at AWords.main(AWords.java:5)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message is telling you exactly what's wrong. Math.pow() returns a double, but BigInteger's methods (not surprisingly) don't deal with doubles. BigInteger has its own pow() method that you should use. And in any case, you can't fit 1000^1000 into a double anyway.

You've got another problem:



The first time through the loop, what do you think is going to happen when you do sum.add? What is the value of sum the first time you do that?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:The first time through the loop, what do you think is going to happen when you do sum.add? What is the value of sum the first time you do that?


@Chaturaka - Hint: If your 'sum' field was an int instead of a BigInteger, what would you initialize it to?

Winston
 
Marshal
Posts: 76856
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You ought not to use Math.pow, because a double will probably run out of range before you reach the 1000th power, unless your value is 0 or ±1.
 
You've gotta fight it! Don't give in! Read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic