Earlier quoted context omitted.
It also leads to some interesting programming errors in Java. class A { public static void main(String[] args) { Integer a = 100, b = 100; Integer c = 10000, d = 10000; System.out.println(a == b); System.out.println(c == d); } } Prints: true false For those unaware, the == check above is doing an Object check (are they the same object). The cache makes the '100' return the same instance of an object. To do actual val…
what a dumb language! operator overloading should have bound the equals method to the == operator without the programmer having to be aware of this. when in java do you ever want to compare if two objects are the same referenced with the same ptr? Never, so why not do the right thing by the user of the language. At least be consistent between primitives and objectives. Sadly, I have to use this language daily. My hea…
Write a program that makes 2 + 2 = 5
61–70 of 76 posts
Re: Write a program that makes 2 + 2 = 5
#62Re: Write a program that makes 2 + 2 = 5
#63Earlier quoted context omitted.
With compiler optimizations turned on, it's almost guaranteed not to happen, because a and b are very likely to be stored in CPU registers.
Or rather, a and b will be constant folded so that the printf call is optimized to "push #4; push ptrFmt; call printf"
Re: Write a program that makes 2 + 2 = 5
#64 local debug = require "debug" -- in case 5.2
debug.setmetatable(1, {__tostring=function(n)
return ("%g"):format(n+1)
end})
print(2+2)
Trying to make 2+2==5 evaluate as true is a lot harder. Although Lua lets you define a custom + operator, it's a fallback not an override.Re: Write a program that makes 2 + 2 = 5
#65 def mad_addr(a,b)
a == b && a == 2 ? (a.object_id + b.object_id)/a : a + b
end
Explanation: If a and b are both equal to 2 we can sum the values of the object_id's (the object_id of 2 is 5), and then divide the sum by 2. [Edit to add] For all other numbers this should behave as expected.I just learned the object_id of 2 is 5 while doing this exercise, and I would like to continue learning more about how Ruby works. So if you have feedback, criticisms, or other ideas regarding this solution please feel free to share :)
[edit: added missing `=`]
Re: Write a program that makes 2 + 2 = 5
#66Ruby solution (don't have enough rep to post to codegolf): def mad_addr(a,b) a == b && a == 2 ? (a.object_id + b.object_id)/a : a + b end Explanation: If a and b are both equal to 2 we can sum the values of the object_id's (the object_id of 2 is 5), and then divide the sum by 2. [Edit to add] For all other numbers this should behave as expected. I just learned the object_id of 2 is 5 while doing this exercise, and I…
It looks like you are missing an equals sign on a == 2.
I just learned the object_id of 2 is 5 while doing this exercise, and I would like to continue learning more about how Ruby works. So if you have feedback, criticisms, or other ideas regarding this solution please feel free to share :)
The object_id appears to be the tagged pointer that contains the value cast to an integer. Ruby stores all of its data types using a single underlying C data type (called VALUE in the Ruby source), using the bottom two bits to identify special values that aren't really pointers. This is why Fixnum can only hold 31 or 63 bits instead of 32 or 64 (including sign), for example:
# Ruby irb on a 32-bit system
> (1 Fixnum
> (1 Bignum
See also:Re: Write a program that makes 2 + 2 = 5
#67Earlier quoted context omitted.
Can anyone explain? It didn't work for me but still gives odd results depending on optimization level that I don't understand.
Because nums is a length 5 array, and the loop condition checks nums[i], GCC assumes going into the loop that i is a valid index, i.e. i It's based on an old Linux bug... I should learn more about GCC internals in order to tell tell why I couldn't get it to work without a loop. In general, any sequence where some property not holding would cause undefined behavior to occur allows a conforming compiler to assume it do…
Re: Write a program that makes 2 + 2 = 5
#68Re: Write a program that makes 2 + 2 = 5
#69Here's a Ruby solution that really does modify the + operation: class Fixnum alias_method :old_plus, :+ def +(*args) old_plus(*args).old_plus(1) end end 2 + 2 #=> 5
Here's one in Ocaml that modifies the + operator let (+) x y = match (x,y) with (2,2) -> 5 | (x,y) -> x+y;;
let (+) 2 2 = 5 in 2+2;;
Re: Write a program that makes 2 + 2 = 5
#70Combining a couple ideas, here's a Go solution http://play.golang.org/p/XnPglbkLW4