• Create Account

## Eight, Nine, Ten...

32 replies to this topic

### #21Cornstalks  Members

7026
Like
2Likes
Like

Posted 19 March 2013 - 12:20 AM

*snip*

Ah, good catch!

I think we should pool our efforts and make a programming language where this kind of thing is sane.

I was hoping for a more concise solution, but here we go (so now you can use commas and hyphens in your numbers!):
#include <type_traits>

template <typename T>
struct Number
{
mutable T n;
Number(T n) : n(n) {}

const Number<T> operator+() const
{
return *this;
}

template <typename U>
const Number<typename std::common_type<T, U>::type> operator, (const Number<U>& num) const
{
return n + num.n;
}

template <typename U>
const Number<typename std::common_type<T, U>::type> operator- (const Number<U>& num) const
{
return n + num.n;
}

template <typename U>
const Number<typename std::common_type<T, U>::type> operator+ (const Number<U>& num) const
{
return n + num.n;
}

template <typename U>
const Number<typename std::common_type<T, U>::type> operator* (const Number<U>& num) const
{
return n * num.n;
}

operator T() const
{
return n;
}
};

Number<unsigned long long> operator"" _n(unsigned long long n)
{
return n;
}

#define ONE      +1_n
#define TWO      +2_n
#define THREE    +3_n
#define FOUR     +4_n
// ... add more if you wish
#define TWENTY   +20_n
#define HUNDRED  *100_n
#define THOUSAND *1000_n
#define AND

#define TIMES *
#define MINUS -

#include <iostream>

int main()
{
// The only real drawback is that you have to surround them with parentheses if you use ,
int x = (FOUR THOUSAND, ONE HUNDRED AND TWENTY-THREE);
int y = (ONE HUNDRED AND ONE);
int result = x TIMES y;
std::cout << x << " * " << y << " = " << result << std::endl;

result = x MINUS y;
std::cout << x << " - " << y << " = " << result << std::endl;
}


Edited by Cornstalks, 19 March 2013 - 12:21 AM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

### #22Felix Ungman  Members

1545
Like
4Likes
Like

Posted 19 March 2013 - 01:28 AM

Good thing they put parentheses around those literals!

edit:

Oh gosh, I didn't even realize the TEN issue... That's horrible.

I don't understand, the only problem is that it's not complete? Here's an excerpt from the full definition:

#define EIGHT  (0x08)
#define NINE   (0x09)
#define AYE    (0x0A)
#define BEE    (0x0B)
#define CEE    (0x0C)
#define DEE    (0x0D)
#define EE     (0x0E)
#define EFF    (0x0F)
#define TEN    (0x10)
...
#define NINETEEN (0x19)
#define AYETEEN  (0x1A)
#define BEETEEN  (0x1B)
...
#define NINETYEFF (0x9F)
...
...
#define EFFTYEFF  (0xFF)


openwar  - the real-time tactical war-game platform

### #23mhagain  Members

12438
Like
-1Likes
Like

Posted 19 March 2013 - 04:10 AM

I think we should pool our efforts and make a programming language where this kind of thing is sane.

It's called "Cobol"...

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

385
Like
0Likes
Like

Posted 19 March 2013 - 11:29 AM

...#define efftyeff (0xff)...

HAHA! Classic!

### #25Vortez  Members

2705
Like
0Likes
Like

Posted 19 March 2013 - 11:30 AM

why someone (sane) would use #define for numbers is beyond me...

### #26BGB  Members

1570
Like
0Likes
Like

Posted 19 March 2013 - 01:00 PM

why someone (sane) would use #define for numbers is beyond me...

things like (in the past):
static const int FOO=...;

tending to result in the compilers creating actual variables, and loading the values from them, to access their constants, as well as hindering compile-time expression evaluation, ... (newer compilers have generally at least gotten a little smarter here). (and, if you forgot the 'static', the linker would complain about multiple definitions, ...)

...

### #27wintertime  Members

3826
Like
0Likes
Like

Posted 19 March 2013 - 01:52 PM

Yeah that thing was always annoying in C. It wont even let you do this:

static const int size=16;
static const int buffer[size];


That const makes it really a constant and not a variable with a constant value is one of those little things you dont even notice until they are not working which C++ added, together with inline functions, function name overloading, new and delete that avoid the manual NULL check, references, ...

#define NULL (0)


#define ONE (1)
#define TWO (2)
...


?

### #28Hodgman  Moderators

49421
Like
0Likes
Like

Posted 19 March 2013 - 05:34 PM

why someone (sane) would use #define for numbers is beyond me...

things like (in the past):
static const int FOO=...;
...
#define FOO _SIZE 42
is one thing, but
#define FOURTY_TWO 42
is a whole different kettle of fish.

### #29L. Spiro  Members

24838
Like
1Likes
Like

Posted 20 March 2013 - 02:24 AM

Fun fact: We use octal a lot more than we realize.

From the C/C++ parser’s viewpoint a number beginning with a 0 is octal.

What is the most common number that begins with 0?

0!

Every time you use a 0 in your code it is seen by the compiler as an octal number.

And macro’ing names of numbers is just silly.

L. Spiro

### #30Alpheus  GDNet+

6811
Like
0Likes
Like

Posted 20 March 2013 - 02:33 AM

What is the most common number that begins with 0?

0!

I almost questioned why you are using 0! (aka. zero-bang, aka. 0 factorial).

External Articulation of Concepts Materializes Innate Knowledge of One's Craft and Science

Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley

If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts

Spoiler

### #31BGB  Members

1570
Like
0Likes
Like

Posted 20 March 2013 - 03:15 AM

What is the most common number that begins with 0?

0!

I almost questioned why you are using 0! (aka. zero-bang, aka. 0 factorial).

but 0!=!0...

random trivia...
not being content with just having NULL, I have a number of other values in the same area:
UNDEFINED //like NULL but not NULL (*3)
BGBDY_TRUE //when you need a boolean TRUE value as a pointer (*3)
BGBDY_FALSE //likewise, but FALSE
AUXNULL //for when you need to express a NULL but NULL and UNDEFINED are already busy (*1)
BGBGC_NULLEND //for when your NULL should really be a 'void' (and a thread has just terminated, *2)
...

*1: AUXNULL==NULL except when AUXNULL!=NULL... yes, sometimes actually useful...
*2: generally to save the cost of keeping around the state to hold a NULL that wont actually be referenced (basically, because you can't "join()" a thread which returns 'void').

*3: UNDEFINED is TRUE for "if()" whereas NULL is FALSE, but sometimes also needed is a FALSE/TRUE that are distinct from NULL/UNDEFINED.

as for naming numbers:
actually, surprisingly, there are people who seem to think this practice is a good idea.

apparently especially off in the land of a certain language unnamed here...

### #32Olof Hedman  Members

5702
Like
0Likes
Like

Posted 20 March 2013 - 04:16 AM

Could be binary coded decimal... [snip]

That sounds so convoluted it requires its own thread.

Unfortunately that wasn't the case here - this code, according to a colleague who originally fixed it - caused a fatal crash in the software.

BCD was pretty common once, there are even x86 assembler instructions that work with BCD numbers!

But I guess that wasn't what was intended here

### #33Khatharr  Members

7695
Like
0Likes
Like

Posted 21 March 2013 - 09:12 PM

When I was around 18 or so I used to hack around the memory of my PSX games with a gameshark (the kind that plugged into the back and allowed memory/debug access - now considered illegal). I remember coming across BCD in some game when I was searching for the address of the HP values or something similar and when I finally realized what was happening I laughed for a solid five minutes at how crazy it seemed. I wish I could remember what game it was.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.