Infinite ranges in C# 1
I recently learned that C# is compliant with the IEEE 754-1985 for floating point arithmetics. That wasn't a big surprise but that division by zero is defined as Infinity in it was! It actually kind of bothers me that I didn't know this.
In mathematics division by zero is undefined for real numbers but I guess Infinity is a more pragmatic result. Or as a friend put it "IEEE stands for Institute of Electrical and Electronics Engineers not Institute of Mathematics"
1 2 3 4 |
double n = 1.0; n = n / 0; if (n > 636413622384679305) System.Console.WriteLine("Yes it certainly is!"); |
This C# code does not throw an exception, it simply leaves n defined as Infinity and writes a line to the console.
Ruby is also IEEE 754-1985 compliant. It even lets you define infinite ranges.
1 2 3 4 5 6 |
Infinity=1.0/0 =>Infinity (1..Infinity).include?(162259276829213363391578010288127) => true (7..Infinity).step(7).take(3).inject(&:+) # 7+14+21 => 42 |
I can't say I see very much use of this but it brings a kind of completeness to the handling of infinities. Unfortunately it seems we don't get that in C# out of the box because Enumerable.Range takes <int>,<int> as parameters and there's no Infinity definition for int. That's unless someone wrote a generic Range class. Turns out none other than Jon Skeet did in his MiscUtil. Download MiscUtil and then by using MiscUtil.Collections; you can:
1 2 3 4 5 6 |
double n = 1.0; var infinity = n / 0; var r = new Range<double>(0, infinity); if (r.Contains(4711)) System.Console.WriteLine("Yes it certainly does!"); var sum = r.Step(7.0).Take(3).Sum(); |
And guess what, it works like a charm! 4711 is part of positive infinity and sum is 42.0 and all is good.
Edit
There's also a couple of predefined constants. Thanks to Eric for pointing that out.
1 2 |
var r = new Range<double>(7, System.Double.PositiveInfinity); var sum = r.Step(7.0).Take(3).Sum(); |
Sierpinski's shoes
There were no cross-platform windowing toolkits for Ruby so _why made one and he calls it Shoes. Not even close to 1.0, it's already yummy in a chunky kind of way and since it came from _why I simply had to try it out. Something simple.
Shoes.app :width => 1024, :height => 768 do
corners = [ {:x => 256, :y => 10}, {:x => 12, :y => 378}, {:x => 506, :y => 378} ]
xpos,ypos,c = 256,10,0
srand
2111.times do
c=rand(3)
xpos += (corners[c][:x]-xpos)>>1
ypos += (corners[c][:y]-ypos)>>1
star xpos, ypos, 5, 10
end
endThe result.
Huge number factored into primes
Recently 2^1039-1 were factored. Is this the end of 1024 RSA encryption? Lenstra, one of the researchers, addresses this question: "Last time, it took nine years for us to generalize from a special to a non-special hard-to factor number (155 digits). I won't make predictions, but let's just say it might be a good idea to stay tuned."
2^1039-1 is a special number that the RSA algorithm would never use so 1024 RSA might still be secure for the time being but if you want to be future safe 2048 bits or more would be the way to go.
Find primes in regexp 5
In an earlier post the example code did find prime numbers. Recently I stumbled over a really cool regexp hack that also deals with primes. This is how you execute that regexp in Ruby:
puts 'Prime' unless ('1' * 43) =~ /^1$|^(11+?)\1+$/Change 43 to whatever you like and you will get Prime as output if it's a prime number. EDIT: As you can see in the comments Neil Kandalonkar explained how the regexp by Abigail works.