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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:57:14+00:00 2026-05-12T14:57:14+00:00

Today, I came across quite strange problem. I needed to calculate string length of

  • 0

Today, I came across quite strange problem. I needed to calculate string length of a number, so I came up with this solution

// say the number is 1000
(int)(log(1000)/log(10)) + 1

This is based on mathematical formula

log10x = lognx/logn10 (explained here)

But I found out, that in C,

(int)(log(1000)/log(10)) + 1

is NOT equal to

(int) log10(1000) + 1

but it should be.

I even tried the same thing in Java with this code

(int) (Math.log(1000) / Math.log(10)) + 1
(int) Math.log10(1000) + 1

but it behave the same wrong way.

The story continues. After executing this code

for (int i = 10; i < 10000000; i *= 10) {
   System.out.println(((int) (Math.log10(i)) + 1) + 
                " " + ((int) (Math.log(i) / Math.log(10)) + 1));
}

I get

2 2
3 3
4 3  // here second method produces wrong result for 1000
5 5
6 6
7 6  // here again

So the bug seems to occur on every multiple of 1000.

I showed this to my C teacher, and he said that it might be caused by some type conversion error during log division, but he didn’t know why.

So my questions are

  • Why isn’t (int) (Math.log(1000) / Math.log(10)) + 1 equal to (int) Math.log10(1000) + 1
    , while it should be, according to the math.
  • Why is it wrong only for multiples of 1000?

edit: It is not rounding error, because

Math.floor(Math.log10(i)) + 1
Math.floor(Math.log(i) / Math.log(10)) + 1

produce same, wrong output

2 2
3 3
4 3
5 5
6 6
7 6

edit2: I have to round down, because I want to know the number of digits.

log10(999) + 1 = 3.9995654882259823
log10(1000) + 1 =  4.0

If I just round, I get same result (4), which is wrong for 999, because it has 3 digits.

  • 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-12T14:57:14+00:00Added an answer on May 12, 2026 at 2:57 pm

    You provided the code snippet

    for (int i = 10; i < 10000000; i *= 10) {
       System.out.println(((int) (Math.log10(i)) + 1) + 
                    " " + ((int) (Math.log(i) / Math.log(10)) + 1));
    }
    

    to illustrate your question. Just remove the casts to int and run the loop again. You will receive

    2.0 2.0
    3.0 3.0
    4.0 3.9999999999999996
    5.0 5.0
    6.0 6.0
    7.0 6.999999999999999
    

    which immediately answers your question. As tliff already argued, the casts cut off the decimals instead of rounding properly.

    EDIT: You updated your question to use floor(), but like casting floor() will round down and therefore drop the decimals!

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

Sidebar

Related Questions

I came across this File parsing programming challenge today and found it quite interesting.
I am quite confused here. Today I came across this concept of AttributedString .
I came across an oddity today that I don't quite understand. Take this code,
Today I came across this problem in javascript and do not know why it
Today I came across a problem where someone had accidentally committed a proj.user file
Today I came across this question: you have a code static int counter =
I have been using Perl for some time, but today I came across this
I came across this today MPI_ERR_COUNT Invalid count argument. Count arguments must be non-negative;
I came across this code today AsyncInvoke(OnTimeMessageTimer, (object)null, (ElapsedEventArgs)null); Is there anything wrong with
Came across this error today. Wondering if anyone can tell me what it means:

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.