Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6756539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:32:11+00:00 2026-05-26T13:32:11+00:00

I have two methods: power and factorial: public static long pow(int x, int n)

  • 0

I have two methods: power and factorial:

public static long pow(int x, int n) {
    long p = x;
    for (int i = 1; i < n; i++) {
        p *= x;
    }
    return p;
}

public static long fact(int n) {
    long s = n;
    for (int i = 1; i < n; i++ ) {
        s *= i;
    }
    return s;
}

that are returning longs. When I want to use them in new method evaluating Exponential function i get wrong results comparing to Math.exp(x). My code:

public static void exp(int x, double eps) {
    int i = 1;
    double pow = 1.0;
    double fact = 1.0;
    double sum = 0.0;
    double temp;
    do {
        temp = pow/fact;
        sum += temp;
        pow = pow(x, i);
        fact = fact(i);
        i++;
    }
    while (temp > eps);
    System.out.println("Check: " + Math.exp(x));
    System.out.println("My: " + sum);
}

public static void main() {
    int x = 10;
    double eps = 0.0000000000001;

    exp(x, eps);
}

and the output for x=10 is:

Check: 22026.465794806718

My: 21798.734894914145

the larger x, the bigger "loss of precision" (not exactly, because you can’t really call it precise…).

The twist is, when methods power and factorial return double then the output is correct. Can anyone explain me how to make it work?

Methods pow and fact must return long and I must use them in exp (college assignment).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:32:12+00:00Added an answer on May 26, 2026 at 1:32 pm

    If you try this pow method:

    public static long pow(int x, int n) {
        long p = x;
        System.out.println("Pow: "+x+","+n);
        for (int i = 1; i < n; i++) {
            p *= x;
            System.out.println(p);
        }
        return p;
    }
    

    You get this output:

    ...
    Pow: 10,20
    100
    1000
    10000
    ...
    ...
    1000000000000000
    10000000000000000
    100000000000000000
    1000000000000000000
    -8446744073709551616
    7766279631452241920
    

    The long value overflows: 10^20 is just too big to fit in a long.

    Methods pow and fact must return long and I must use them in exp (college assignment).

    Then there is not much you can do to fix it. You could throw an exception if eps is too small.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two methods: Public Function GetTotalLimit(ByVal entity As Entity) As Int64 Return (From
I have two methods that are overloads of each other public class Car {
Suppose i have two methods in a class say public void eat(int i,String s)
I have two methods that both return an IObservable IObservable<Something[]> QueryLocal(); and IObservable<Something[]> QueryWeb();
Let's say that I have two methods defined with the same name and return,
I have two methods, one that I use to convert an image to a
I have two action methods that are conflicting. Basically, I want to be able
I have these two methods on a class that differ only in one method
I have two methods f(vector<int>& x, ....) and g(DBConn& x, ....) where the (....)
I have two methods that do the same thing. The first one makes performance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.