473,508 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bug with adding numbers with unsigned long long var

2 New Member
Expand|Select|Wrap|Line Numbers
  1. ...
  2. #include <cmath>
  3. ...
  4.  
  5.  
  6. unsigned long long X, j , i, total = 0;
  7. cin >> X;
  8.  
  9.  
  10. for ( i = 0 ; i < 18 ; ++i )
  11. {
  12.     total += 9*(i+1)*pow(10,i);
  13.     //cout << "total : " << total << endl;
  14.     if ( total >= X ) break;
  15.     }
  16.  
  17. cout << total ;
  18.  
  19.  
  20.  
For input 40000 it gives me wrong answer. It should be 488889, but result is 488887. Also with input 3000 gives wrong thing. I'm working on Windows, code blocks and it doesn't work when I put g++ follow C++11 standard ( in settings -> compiler ). Warning is that ISO C++ 1998 does not support long long. Anybody can help me with this ? ty :)
Oct 24 '13 #1
4 1904
Nepomuk
3,112 Recognized Expert Specialist
Hi jures and welcome to bytes.com!

I just checked my local g++ installation and it doesn't seem to support the C++11 standard; C++98 seems to be the closest I can get. Compiling with that I had to change a few things as the pow function isn't defined for an int and a long long value. Here's what I came up with:
Expand|Select|Wrap|Line Numbers
  1. #include<cmath>
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.         unsigned long long X, j, total = 0;
  8.         int i;
  9.         cin >> X;
  10.  
  11.         for ( i = 0 ; i < 18 ; ++i ) {
  12.                 total += 9*(i+1)*pow(10.,i);
  13.                 //cout << "total : " << total << endl;
  14.                 if ( total >= X ) break;
  15.         }
  16.  
  17.         cout << total << endl;
  18. }
  19.  
You will notice two slight differences to your code: i is an int rather than an unsigned long long and the first argument of that pow function is now a double value (due to the point after the digits). This not only compiles but for the input 40000 it indeed returns 488889 as you expect. If I take out the commented line, the output is as follows:
Expand|Select|Wrap|Line Numbers
  1. total : 9
  2. total : 189
  3. total : 2889
  4. total : 38889
  5. total : 488889
  6. 488889
  7.  
So, the question is: Does that code work for you and if not, where does it go wrong?
Oct 24 '13 #2
jures
2 New Member
oh , I see :D
meanwhile, I lost my temper and just installed older version and it works with long long. Yes, code works exactly as I wish. Ty :)
Oct 24 '13 #3
Nepomuk
3,112 Recognized Expert Specialist
An older version works while a newer one doesn't? What a weird world we live in... ;-)
Oct 24 '13 #4
donbock
2,426 Recognized Expert Top Contributor
Use of floating-point always carries the risk of imprecise results. @Nepomuk has shown that this doesn't have to be the case in your specific case; but the general point remains: use floating point if you have to but always consider if you can manage with integers.

You can replace the pow() function like this:
Expand|Select|Wrap|Line Numbers
  1. long long multiplier = 1;
  2. ...
  3. for ( i = 0 ; i < 18 ; ++i ) {
  4.    total += 9*(i+1)*multiplier;
  5.    multiplier *= 10;
  6.    //cout << "total : " << total << endl;
  7.    if ( total >= X ) break;
  8. }
  9.  
This change has two benefits: you eliminate any chance of imprecision introduced by floating point arithmetic; and your code will run faster.
Oct 25 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
6893
by: George Marsaglia | last post by:
The essence of a multiply-with-carry RNG is to have declarations in the RNG proc, such as unsigned long mwc( ){ static unsigned long x = 123456789, c = 362436; unsigned long long t, a =...
24
1836
by: Michael B Allen | last post by:
I use long longs occasionally. I know there are some limitations regarding the standards such as not using long long constants but what's the big deal? Why is long long not used so much? Mike
12
5415
by: Peter Ammon | last post by:
When I add an unsigned long long and an int, what type do each of the values get promoted to before the addition is performed? What is the type of the resulting expression? What occurs if the...
5
9115
by: Daniel Rudy | last post by:
How does one covert a interger number in a unsigned long long int (64-bit) to long double (80-bit) storage? I looked at math.h and I found function that convert double to long long, but didn't...
36
5260
by: Digital Puer | last post by:
Hi, suppose I have an unsigned long long. I would like to extract the front 'n' bits of this value and convert them into an integer. For example, if I extract the first 3 bits, I would get an int...
9
3921
by: luke | last post by:
Hi everybody, please, can someone explain me this behaviour. I have the following piece of code: long long ll; unsigned int i = 2; ll = -1 * i; printf("%lld\n", ll);
3
4054
by: wenmang | last post by:
Hi, I encountered a problem involving storage of unsigned long long. The requirement of incoming data field is an unsigned 64-bit integer, but on our system, due to lack of support of 64-bit...
3
13167
by: Nicholas Zhou | last post by:
Hi, I was writing a testing program to test the ranges of char, short, int and long variables on my computer, both signed and unsigned. Everything was fine except for unsigned int and unsigned...
21
2599
by: Bart C | last post by:
I've always had a problem knowing exactly how wide my integer variables were in C, and the little program below has increased my confusion. Run on 3 compilers on the same cpu (32-bit pentium),...
17
7518
by: Tarique | last post by:
This program was compiled on MS Visual C++ 08 /*Fibonacci Numbers*/ #include<stdio.h> #include<limits.h> void fibonacci(int n) { unsigned long long fib0 = 0; /*First Fibonacci Number*/
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.