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 8720977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:08:49+00:00 2026-06-13T07:08:49+00:00

I have the following, working method in Java: /** * Determines if n is

  • 0

I have the following, working method in Java:

/**
 * Determines if n is a power of z
 * 
 * @param z the number that n may be a power of
 * @param n the number that may be a power of z
 * @return true if n is a power of z 
 */
public boolean isPowerOf(int z, int n) {
    double output = Math.log(n) / Math.log(z);
    if(output % 1 > 0) {
        return false;
    } else {
        return true;
    }
}

isPowerOf(3, 729); //returns true, because 3^6 = 729

Works fine n mighty, but I tried it differently the first time:

public boolean isPowerOf(int z, int n) {
    double output = Math.log(n) % Math.log(z);
    if(output != 0) {
        return false;
    } else {
        return true;
    }
}

However, for log(729) % log(3) seems to return 1.0986122886681093, while the outcome of log(729) / log(3) is 6.

Anyone able to tell me what causes the modulo operator to still give 1.09 remainder here?

  • 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-06-13T07:08:49+00:00Added an answer on June 13, 2026 at 7:08 am

    Anyone able to tell me what causes the modulo operator to still give 1.09 remainder here?

    The normal floating point inaccuracies, basically. The values you’re using aren’t exactly log(729) and log(3). If you look at log(3) and log(729) % log(3) you’ll see they’re almost exactly the same:

    public class Test {
        public static void main(String[] args) {
            double x = Math.log(729);
            double y = Math.log(3);
            System.out.println(x);
            System.out.println(y);
            System.out.println(x % y);
        }
    }
    

    Output:

    6.591673732008658
    1.0986122886681098
    1.0986122886681093
    

    In other words, log(729) is effectively log(3) * 5.9999999999999 (or something similar). You’ll probably want to add some tolerance to your test, basically, and return whether or not the remainder is very close to 0 or very close to the log(z).

    Alternatively, use log and division to work out “roughly” what the power should be, then Math.pow to check for the exact value:

    int power = (int) (Math.log(n) / Math.log(z) + 0.5);
    return n == Math.pow(z, power);
    

    Here you should be okay in terms of floating point inaccuracies until the numbers get “pretty big”. You can use BigInteger if you want to cope with very big numbers precisely.

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

Sidebar

Related Questions

Constructor and method not working as expected in Java program I have the following
I have the following method being called in a Java EE web application. public
I have the following: An environment that is working like a hash for rows
So I have been working on a 2 player Tic-Tac-Toe game in java that
I have the following setup: SD card Mount Receiver (MountReceiver.java) public IntentFilter getIntentFilter() {
I'm working with JPA 2 and have the following method: private static void wipeTable(EntityManager
i have the following class that writes to a file but is not working
I have the following working tar -pcvf base.tar input/myPacket/my2 --exclude-vcs input/myPacket/my3/*.bmp When i have
In YUI I have following code working for mouse wheel. How do I make
We have the following code working for a complex rails form with checkboxes. I'm

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.