r/programming • u/lifthrasiir • Jul 10 '11
Spoken Number to Decimal, in 256 bytes of C
http://j.mearie.org/post/7462182919/spoken-number-to-decimal
218
Upvotes
r/programming • u/lifthrasiir • Jul 10 '11
128
u/__s Jul 10 '11
Spelt, not spoken
Which I prefer. I was worried it'd be some cheap wrapper around a speech to text library
5
u/fermion72 Jul 11 '11
What's going on here?
If a is less than zero, printf. If not, call
main()? Why doesn't this give an error thatmainisn't defined correctly? I.e., doesn't main have to have four arguments, as per the way it is defined at the start of the program?23
u/joehillen Jul 10 '11
/r/tinycode
12
u/ex_ample Jul 11 '11
Spoken would have been pretty impressive.
11
u/[deleted] Jul 11 '11
I wrote one of these as a C++ assignment in school. Here's some output:
cheque.exe $389,854,621,754,139,584,752,136,548,919,753,731,988,524,563,174,585,214,569,873,216,549.43
Output:
Three hundred and eighty nine vigintillion eight hundred and fifty four novemdecillion six hundred and twenty one octodecillion seven hundred and fifty four septendecillion one hundred and thirty nine sexdecillion five hundred and eighty four quindecillion seven hundred and fifty two quattuordecillion one hundred and thirty six tredecillion five hundred and forty eight duodecillion nine hundred and nineteen undecillion seven hundred and fifty three decillion seven hundred and thirty one nonillion nine hundred and eighty eight octillion five hundred and twenty four septillion five hundred and sixty three sextillion one hundred and seventy four quintillion five hundred and eighty five quadrillion two hundred and fourteen trillion five hundred and sixty nine billion eight hundred and seventy three million two hundred and sixteen thousand five hundred and forty nine dollars and forty three cents.
4
u/ednedn Jul 11 '11
Now as a simple exercise to the reader, do the same with numbers in French.
1
u/quotability Jul 11 '11
for zero, he could have just had it done nothing at all. and why 'r' for zero? why not 'z'? that's unique. 'r' is not.
1
u/Vigud Jul 11 '11 edited Jul 11 '11
In the shorter version, inside the third loop, you start with n set to 17. Then, inside its block, you write: u = 1 << 198 % n-- * 4; which would be 1 << 44, which is way too much for a 32-bit int. Now, I don't understand your magic there, but I've changed it to start with 16 and it still produces correct output for the maximum (19,999,999) [edit: which seems to not be the maximum after all? The shorter version can handle "seventy nine millions nine hundred ninety nine thousands nine hundred ninety nine" on my system where int is 32-bit].
And n == 10 is problematic as well... Hmm.
[edit: Because I don't fully understand the purpose of n = 17, I suggest replacing u=1<<198%n--*4; with u=1<<198%n--*4%32; instead of replacing 17 with 16. For n == 10 and n == 17, u has the same value for both versions with %32 and without it. At the cost of 3 bytes of code, you avoid undefined behavior in that statement.]
Secondly, I believe you can replace n++||(e=getchar()|32)>=0; with n++||(e=getchar()|32)>0; which would be 1 byte shorter :)
1
u/quotability Jul 12 '11
Interesting that it is 256 bytes of C. What's the size of the compiled program?
6
u/olov Jul 10 '11
impressive
2
u/amstan Jul 10 '11
15 and nyn (an) (0)
Nyan!
-1
u/[deleted] Jul 11 '11
This is so effing creative it hurts. Someone make a painting of this so I can put it on my wall...
-10
u/Kinglink Jul 11 '11
Every time I start to read code like this I stop. It's the first thing that needs to be drummed into every programmer just like it had to be drummed into me.
Writing your code with out line breaks doesn't optimize shit.
Making your code unreadable (unnamed variables) doesn't make your code more optimized most of the time.
If your code isn't legible, it's worthless.
I read his writing and it's very interesting, I just wish he had some code I could read.
0
u/[deleted] Jul 11 '11
[deleted] — view deleted comment