On Suday the 11th of February, 2007 -after 3 and a half years on InvisionFree, we have moved! This old board remains as a read only archive of years past, and registration has been disabled here. All new and current members should register at http://www.cpplc.net/forum .
| · Portal |
Help
Search
Members
Calendar
|
| Welcome Guest ( Log In | Register ) | Resend Validation Email |
| Welcome to C++ Learning Community. We hope you enjoy your visit. You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free. Join our community! If you're already a member please log in to your account to access all of our features: |
| Pages: (2) [1] 2 ( Go to first unread post ) | ![]() ![]() ![]() |
| KTC |
Posted: Sep 6 2005, 07:13 AM
|
||
|
std::freak Group: Members Posts: 1,465 Member No.: 420 Joined: 30-December 03 |
This is lifted straight out of an article in CUJ, but I thought it might be a interesting challenge for people here. Who would be prepare to try their hands at writing a (small) program that they are confident will work correctly the first time? No testing, no debugging. The problem: How many decimal digits are in a given unsigned integer?
The problem (slightly more difficult): Same as above, but does not involve recursion. The problem (more difficult): Same as aboves, but with signed integers. p.s. I'll post a link to the article later, when people have had a try at it. -------------------- Say NO to software patents !!
Newbie-FAQ : Archive of Informative Topics The D Programing Language : High Level Assembly |
||
| C-Man |
Posted: Sep 6 2005, 09:14 AM
|
||
|
Lazy bum Group: Super Moderator Posts: 4,563 Member No.: 609 Joined: 29-February 04 |
don't blame me if it doesn't compile -------------------- |
||
| ramirez |
Posted: Sep 6 2005, 09:32 AM
|
||
|
geekoid Group: Members Posts: 403 Member No.: 1,218 Joined: 18-February 05 |
I basically did the same as C-Man with for loop. :P
|
||
| dr voodoo |
Posted: Sep 6 2005, 10:20 AM
|
||
|
c++ programmer Group: Super Moderator Posts: 1,986 Member No.: 11 Joined: 23-June 03 |
Does the minus sign count or not? I'll assume that the number of characters needed for the representation produced by printf("%i", i); is meant.
-------------------- Conscience doesn't really keep you from doing anything wrong -- it merely keeps you from enjoying it.
My Site My Tutorial |
||
| ramirez |
Posted: Sep 6 2005, 01:01 PM
|
|
geekoid Group: Members Posts: 403 Member No.: 1,218 Joined: 18-February 05 |
Well, the problem is that how many digits there are, as far as I know a minus sign isn't considered a digit.. :P
But yeah, it's a good question whether it should be counted or not. |
| KTC |
Posted: Sep 6 2005, 04:23 PM
|
||||||||||
|
std::freak Group: Members Posts: 1,465 Member No.: 420 Joined: 30-December 03 |
The specification (problem statement) doesn't specify, so it's your job as a programmer to decide and then document the decision. Comments on proposed solutions: C-Man's:
An implementation is allow to truncate the value of n/10 either up or down. So (-99)/10 can return -9 or -10. Thus your solution does not always work. ramirez's:
Same problem as C-Man. dr voodoo's:
A negative integer does not always have a positive corresponding absolute value that's representable, 2's complement arithmetic. If I pass INT_MIN as the argument to your function, on a lot of computers, your function wouldn't return a correct result.
signed unsigned mismatch.
Guys, there's a reason why the signed integer version is listed as the most difficult of the three. Maybe try the simpler one first? -------------------- Say NO to software patents !!
Newbie-FAQ : Archive of Informative Topics The D Programing Language : High Level Assembly |
||||||||||
| myork |
Posted: Sep 6 2005, 04:54 PM
|
||||
|
c++ guru Group: Super Moderator Posts: 2,584 Member No.: 487 Joined: 21-January 04 |
My first thought was:
But you can not tell where to stop. How many digits an int holds depends on the platform so you don't know how many if's to add. Thinking about this so more. You may be able to use some form of template meta program to solve that problem. so
|
||||
| myork |
Posted: Sep 6 2005, 05:14 PM
|
||
|
c++ guru Group: Super Moderator Posts: 2,584 Member No.: 487 Joined: 21-January 04 |
Just guessing. |
||
| C-Man |
Posted: Sep 6 2005, 05:33 PM
|
|
Lazy bum Group: Super Moderator Posts: 4,563 Member No.: 609 Joined: 29-February 04 |
KTC because i know 100% how x86 handles integer division i know it will allways work
on x86 -------------------- |
| KTC |
Posted: Sep 6 2005, 05:36 PM
|
|
std::freak Group: Members Posts: 1,465 Member No.: 420 Joined: 30-December 03 |
I never said you're programming on / for a x86 platform. We're writing a C++ program. I wanted a C++ program that's guarantee to work based on the C++ standard, and as I told you in IRC just now, the C++ standard doesn't guarantee such division to work as you intended. ISO/IEC 14882:2003 section 5.6.4.
-------------------- Say NO to software patents !!
Newbie-FAQ : Archive of Informative Topics The D Programing Language : High Level Assembly |
| dorto |
Posted: Sep 6 2005, 06:09 PM
|
||
![]() geek Group: Members Posts: 268 Member No.: 1,143 Joined: 20-December 04 |
how x86 handles the integers is irrelevant here i guess. how C++ compiler handles that thing is more important and that you can never tell. -------------------- dorto
Absolute Beginner(programming): 'You Can Do It' by Francis Glassborow Absolute Beginner(c++): 'Accelerated C++' by Andrew Koenig and Barbara Moo Free Online Book: http://mindview.net/Books/TICPP/ThinkingInCPP2e.html C++ Bible: 'The C++ Programming Language' by Bjarne Stroustrup |
||
| C-Man |
Posted: Sep 6 2005, 07:55 PM
|
|
Lazy bum Group: Super Moderator Posts: 4,563 Member No.: 609 Joined: 29-February 04 |
So basing on that KTC , C/C++ isn't portable ? since the same operation would produce different results on different platforms ? no ?
-------------------- |
| myork |
Posted: Sep 6 2005, 08:14 PM
|
|
c++ guru Group: Super Moderator Posts: 2,584 Member No.: 487 Joined: 21-January 04 |
If you wonder off the edge of the well defined parts of C/C++ it becomes undefined and thus non portable. Thus when your code comes up against (or close too) the edges of the standard you need to be extra carefull.
Most normal code does not wonder around these wild and dangerouse edges. But if you start writting libraries you need to be more carefull. |
| KTC |
Posted: Sep 7 2005, 01:42 AM
|
||||||||||||||||
|
std::freak Group: Members Posts: 1,465 Member No.: 420 Joined: 30-December 03 |
myork's: You changed your solution to the signed version after you initially posted it, cheat!
I'm never asking you to write a library ever, if that's your solution!
That works.
I came up with basically the same idea this afternoon, and for all practical purposes, this works (i.e. it works on every implementation that exists). However, on closer check of the C++ standard, I discovered this:
In another word, there is no guarantee that unsigned int can hold ~x, where x is an int. So, the following sentence is not necessarily always true.
Also, regarding
We have this:
So that assumption doesn't hold either.
Anyway, the article where I got this from is here: http://www.cuj.com/documents/s=8188/cuj0402koenig/ (If you don't have an account, sign up for free 6 months membership, or use http://www.bugmenot.com/) I'll have to think a little bit more about an solution for signed integer. -------------------- Say NO to software patents !!
Newbie-FAQ : Archive of Informative Topics The D Programing Language : High Level Assembly |
||||||||||||||||
| myork |
Posted: Sep 7 2005, 01:53 AM
|
|
c++ guru Group: Super Moderator Posts: 2,584 Member No.: 487 Joined: 21-January 04 |
OK. SO the unsigned versions holds.
Need to think about the signed version. |
Pages: (2) [1] 2 |
![]() ![]() ![]() |