Wednesday, January 25, 2006

I can't believe it's not truncating!

This is for developers only. If you are not a developer but keep reading, you will just think this is dumb--my wife certainly did.

I have always been taught that integer arithmetic yielded truncated results. For example, 10+0.9 = 10. The reason for this is that 10 is an integer and thus 0.9 is converted to an integer before the addition takes place. This conversion is performed by truncated the fractional part (.9), leaving just 0. 10+0=10.

Trust me, as a computer engineering student, this is hammered home over and over again.

Fast forward to the jobsite. I am using real technologies now like VB.Net. I am using a method that returns a double/float and really need the integer part. So, I use good old CINT(double). I am shocked when my results appear to be rounded! Holy crap!

This is huge. CINT(1.6) = 2, not 1! So I look up the documentation and sure enough:

Note: CInt differs from the Fix and Int functions, which truncate, rather than round, the fractional part of a number. When the fractional part is exactly 0.5, the CInt function always rounds it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2.


I'll be damn. You might notice something even more crazy--it doesn't just round .5 up, it rounds to the nearest even number. Holy crap times two. I have not used this style of rounding since chemistry class in college when we had to mess with significant digits and such. This means that CINT(2.5) = 2...and...CINT(1.5) = 2. Yes, they both equal 2.

OK, I know you are thinking that the madness must be over...it's not. The documentation suggests that you use INT or FIX instead. Now skeptical of all things, I lookup INT. It seems that this has its quarks, too. INT always rounds DOWN--even for negative numbers! That is, INT(1.9) = 1, INT(-1.1) = -2.

It turns out that FIX is the function of choice for good old fashioned integer truncation. Go figure.

It should be noted that as far as I can tell, integer casting in C/C++ works like FIX...like everyone I know expected.

A conversation I had with Michael today

Michael: in computers, what is 1*1.5?

sarah: 2

Michael: good guess
no

sarah: 1
?

Michael: it is typically 12
1
1
integer arith.

sarah: ah
i forgot about that

Michael: i just discovered a HUGE thing in VB that shocked me

sarah: so 1*1.9 is 1, too

Michael: yes

sarah: what's that?

Michael: CINT(1.9) is used to convert the argument to an integer
so, i would expect...
CINT(1.9) = 2
wait
I would expect cint(1.9)=1

sarah: ya
no?

Michael: it doesn't. Cint ROUNDS....holy shit

sarah: :)

Michael: i called bryan. he didn't believe me

sarah: crazy!

Michael: this is so backwards

sarah: you guys are nerdy
:)

Michael: seriouslyt

sarah: i know!

Michael: it gets worse

sarah: it's so awesome!
and crazy!
crazily awesomely crazy!

Michael: it uses scientific rounding -- it rounds .5 to the nearest even!
cint(1.5) = 2
cint(2.5) = 2

sarah: omg no way!

Michael: so to go from assuming it simply truncated to now rounding ina very unusual way is very disconcerting
you think you are funny but you are not.

Tuesday, January 24, 2006

Thoughts About Pudding

  • I think pudding is capable of ending all wars and conflicts.

  • I think it is weird that I can buy both refrigerated and non-refrigerated pudding.

  • Cafeteria pudding usually sucks. That's because it's not real pudding.

  • Low-cal pudding isn't very good either, but it will doĆ¢€”when it comes to pudding, take what you can get.

  • You can eat pudding with a spoon or fork. It's so versatile!


And one final random though (not related to pudding)...

  • Why is it that it takes hours and hours to freeze a slice of lasagna but I can reheat the same frozen block in only 90 seconds? Come on!

Monday, January 2, 2006

Things my dog has eaten today

a peanut m&m
half of a squeaker from a toy (subsequently coughed up)
fluff from the toy
a jello cup (mostly empty)
a potato