|
-
September 20th, 2005, 07:54 PM
#1
itoa replacement ALMOST perfect
This code is almost perfect. The only problem occurs (commented in code below). The problem does not occur if T is an int and n is int_min. Little help please. There is a solution to this because ostringstream gets it right.
PHP Code:
#define int64_max 9223372036854775807LL
#define int64_min (-int64_max-1)
template <typename T>
aStr IntToStr(T n, uint radix= 10)
{
if(!n)
{
return "0";
}
else
{
bool neg= n < 0;
if(neg)
{
n= -n;
MB(n); //when T is __int64 and n is int64_min, n stays as int64_min!
}
const uint bits= sizeof(T)*8;
char buf[bits];
// Set chars from the end of the buffer
char* ptr= &buf[bits-1];
*ptr= 0; //terminating NULL
while(n > 0)
{
--ptr;
//*ptr= itoaChars[n%radix]; //this works too
*ptr= n%radix+'0';
n /= radix;
}
if(neg)
{
--ptr;
*ptr= '-';
}
return aStr(ptr);
}
}
-
September 20th, 2005, 09:17 PM
#2
Re: itoa replacement ALMOST perfect
please show the error message
-
September 20th, 2005, 09:50 PM
#3
Re: itoa replacement ALMOST perfect
There is no error, it just outputs jibberish such as: x*((u)60($
-
September 20th, 2005, 10:48 PM
#4
Re: itoa replacement ALMOST perfect
 Originally Posted by aewarnick
This code is almost perfect.
No it isn't. See comments below.
The only problem occurs (commented in code below). The problem does not occur if T is an int and n is int_min.
You should be using the constants defined in <limits.h> and not hard-code minimum and maximum values. Not everyone has a 64-bit machine to test your code on.
What is wrong with just using itoa? Is it for the same (possibly wrong) reason you wanted to code your own gcvt from this thread?
http://www.codeguru.com/forum/showthread.php?t=357633
Quite honestly, I don't look too seriously on code that is supposed to replace standard library functions. Usually the code that is supposed to "replace" the standard library functions is inferior to the standard library code in maintainability, speed, and a whole host of other areas.
Right now, you have a bug, so already you are going uphill when you don't need to. I already pointed out that your code is only for 64-bits. What if you want to take this code to a 32-bit machine? So in reality, you haven't replaced itoa, you are trying to write a limited subset of itoa.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; September 20th, 2005 at 11:18 PM.
-
September 21st, 2005, 05:46 AM
#5
Re: itoa replacement ALMOST perfect
given that this is C++ (you've used a template) why not use standard libraries?
What is aStr? What is MB?
-
September 21st, 2005, 09:10 AM
#6
Re: itoa replacement ALMOST perfect
One of the reasons I'm making this code is that itoa is inferior. It only accepts int.
Now that my motive is clear...I am using a 32 bit machine, I don't see where you are getting the idea that the code only works for 64 bit. The code does work perfectly for int32_max, uint32_max, uint64_max, int64_max...all the time. It's only when n is at a negative max value.
When n is -2147483648 and I do this:
n= -n;
the max of int is 2147483647, 2147483648 is too big, so it just remains the same; -2147483648. And that is where the problem is.
Last edited by aewarnick; September 21st, 2005 at 09:54 AM.
-
September 21st, 2005, 09:19 AM
#7
Re: itoa replacement ALMOST perfect
You can always use a stringstream:
Code:
std::stringstream ss;
ss << my_int;
Just like Paul McKenzie said, I'm a bit reluctant when it comes to rewrite standard functions...
-
September 21st, 2005, 10:05 AM
#8
Re: itoa replacement ALMOST perfect
What if I want to use this in a C not C++ library sometime in the future?
Lets look at this from a different angle - You make a convert to absolute value function that works for any integer type; int, int64, int128...
PHP Code:
template<typename T>
T Abs(T t)
{
return -t;
}
MB(Abs(int_min)); //output -2147483648 WRONG!
MB(Abs(int_min+1)); //output 2147483647
How would you solve that? Keep in mind that you can't use abs because that function does not accept __int64.
-
September 21st, 2005, 11:14 AM
#9
Re: itoa replacement ALMOST perfect
 Originally Posted by aewarnick
What if I want to use this in a C not C++ library sometime in the future?
1) This is a C++ forum.
2) Since you posted a template, it is assumed you are using this in C++, not C. Therefore you will get C++ answers. Next time state up front you are writing this for a C compiler. No one can read minds.
Regards,
Paul McKenzie
-
September 21st, 2005, 11:16 AM
#10
Re: itoa replacement ALMOST perfect
 Originally Posted by aewarnick
One of the reasons I'm making this code is that itoa is inferior. It only accepts int.
Now that my motive is clear...I am using a 32 bit machine, I don't see where you are getting the idea that the code only works for 64 bit.
Code:
#define int64_max 9223372036854775807LL
This will not compile on a compiler where the maximum int is 32 bits. In other words, your code is highly non-portable.
You never write code assuming these types of limits. You always use the constants defined in limits.h to determine what is the maximum integer supported by the compiler.
Regards,
Paul McKenzie
-
September 21st, 2005, 11:34 AM
#11
Re: itoa replacement ALMOST perfect
 Originally Posted by aewarnick
One of the reasons I'm making this code is that itoa is inferior. It only accepts int.
Just to add,
The itoa() is a 'C' function that cannot be overloaded. Therefore it can only have one type, and the type that is chosen is int. An "int" is the standard integer type for C++ where the sizeof(int) is compiler dependent. There is no such standard type as int64, __int64, or anything like that. Yes, you've used these types, but all of those are compiler creations and not officially part of the ANSI standard.
So saying that itoa() is inferior is not a fair statement. If your compiler library vendor were smart, there would be functions called itoa_64() or abs_64() or something similar to that if the compiler has 64 bit numbers. I would be highly surprised if the compiler didn't have these functions, given that the compiler has native 64-bit integers.
Regards,
Paul McKenzie
-
September 21st, 2005, 11:41 AM
#12
Re: itoa replacement ALMOST perfect
Just wanted to mention that itoa is not standard. Writing a version of it that uses only standard code and is portable is not a bad idea, although I personally just prefer to use a stringstream.
-
September 21st, 2005, 11:42 AM
#13
Re: itoa replacement ALMOST perfect
I'll start using limits.h. That's a good point. I am using MSVC+ 2002. I can't find 64 bit functions anywhere.
-
September 21st, 2005, 11:52 AM
#14
Re: itoa replacement ALMOST perfect
I'd use <numeric_limits> myself but then I'm a standard C++ programmer.
-
September 21st, 2005, 01:31 PM
#15
Re: itoa replacement ALMOST perfect
 Originally Posted by aewarnick
I'll start using limits.h. That's a good point. I am using MSVC+ 2002. I can't find 64 bit functions anywhere.
According to the MSDN, the following functions are available:
_itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|