Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-25-2007, 06:16 PM   #1 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 203
falsepride is on a distinguished road
need help debugging

Code:
#include <iostream.h>
#include <stdio.h>
#include <cmath>

typedef unsigned long ULONG;
typedef unsigned short int USHORT;

void user_wait();
void user_wait()
{
     int c;
     cout << "Press enter to continue . . . ";
     do
     {
          c = getchar();
          if(c == EOF) break;
     } while(c != '\n');
}

int main()
{
    ULONG levelExp(99);
    ULONG exp, a;
    USHORT desiredLevel;
    a = 0;
    for (int i = 1; i < 100; i++)
    {
        a += i+(300*pow(2,(i/7)));
        //levelExp[i-1] = a/4;
        cout << a/4 << "\n";
    }
    
    cout << "Enter your mage exp:";
//cin >> exp;
    cout << "\n";
    cout << "Enter your desired level:";
//cin >> desiredLevel;
    cout << "\n";
    user_wait();
    return 0;
}
i just started learning c++, actually my friend lent me his book 1 week ago exactly. so i asked another who plays a rpg called runescape if there was any calculators he wanted so i could make a program for practice. thats what thats a start off. but when i go to compile i get a error pointing to "a += i+(300*pow(2,(i/7)));" and something about going from DOUBLE TO ULONG
falsepride is offline   Reply With Quote
Old 04-26-2007, 03:42 AM   #2 (permalink)
ShadyCraig
Recruit
 
Join Date: Sep 2005
Location: Loughborough, England, UK
Posts: 12
ShadyCraig is on a distinguished road
It would seem that the result of that expression would be too big to fit in the ULONG variable 'a'.

Try redeclaring 'a' as a double:
ULONG exp;
double a;
ShadyCraig is offline   Reply With Quote
Old 04-26-2007, 11:28 AM   #3 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 203
falsepride is on a distinguished road
13,034,431 is how much exp it takes for level 99. at max a will be 4 times that so it'll only reach 52 million. unsigned long integers can handle that much.
falsepride is offline   Reply With Quote
Old 04-26-2007, 12:54 PM   #4 (permalink)
ShadyCraig
Recruit
 
Join Date: Sep 2005
Location: Loughborough, England, UK
Posts: 12
ShadyCraig is on a distinguished road
according to this source pow() returns a double:
C Guide--2.7 math.h

if you really don't want to use a double then maybe you could cast the returned variable to a ULONG?
a += i+(300*(ULONG)pow(2,(i/7)));
ShadyCraig is offline   Reply With Quote
Old 04-29-2007, 03:51 PM   #5 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
if you cast it afterwords you've still lost accuracy (since your double overflowed). The main reason the library pow function is quick though is because it knows how to take advantage of the fpu. Since your data type needs are well defined, the easiest method might be to just define your own unsigned long integer pow.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 05-06-2007, 04:56 PM   #6 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 203
falsepride is on a distinguished road
when using shadys answer the numbers didn't come out to what they should be, so tekno how would i do that. the book i reading seems to be more heavy on theory, or teaching how to think in programming. it tells how to polymorph, and opp, pointers, some detail on how computer memory/processing works, how the free store works. but almost halfway through the book and the only library functions it went through are cin and cout. so i don't really know how to do what you said,
falsepride is offline   Reply With Quote
Old 05-06-2007, 10:51 PM   #7 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Well the reason the numbers didn't come out was because the built in pow works with floats or doubles, which are too small to hold your number, so you can define a new version of pow that works with unsigned longs which can hold your large numbers.
Code:
unsigned long ulpow( unsigned long base, unsigned int exponent )
{
  unsigned long result = 1;
  while( exponent > 0 ){
    result *= base;
    exponent--;
  }

  return result;
}
That's just a simple translation of the definition of the power function, but it uses the unsigned long so we know it won't overflow with the size of the numbers you need. It's going to be marginally slower than the standard pow, and it doesn't handle as many cases (like fractional exponents or negative exponents). Since your exponent is (i/7), we can't guarantee that's going to be integral so we should modify it a to be a bit more accurate.
Code:
unsigned long ulpow( unsigned long base, unsigned long double exponent )
{
  unsigned long result = 1;
  unsigned int integralExp = exponent;
  long double fractionalExp = exponent - (long double)integralExp;
  if( fractionalExp ){
    /* This is still bad since the inexactness here will add up to a lot later*/
    result = powl((long double)base,fractionalExp);
  }
  while( exponent > 0 ){
    result *= base;
    exponent--;
  }

  return result;
}
It's still going to be wrong because it drops significant figures, for example:
pow(2.0,2.5) = 5.565
ulpow(2,2.5) = 4
But for integral values of (i/7) it will be correct, and it should be more correct on large values than the overflowed version. Perhaps smarter rounding might help. I guess it depends on how accurate you need to be.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help debugging. please. im lost falsepride HTML, XML, Javascript, AJAX 2 06-20-2005 07:25 PM
No ASP debugging AlexB1234 MS Technologies ( ASP, VB, C#, .NET ) 1 04-19-2005 01:07 PM
Problems debugging visual c++ in vs.net 2003 Engineer MS Technologies ( ASP, VB, C#, .NET ) 2 07-20-2004 12:19 PM
Help debugging a power problem Belisarius Lounge 0 10-25-2003 04:44 PM
Debugging? Admin PHP 3 05-14-2002 03:09 PM


All times are GMT -8. The time now is 12:28 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting