[Tutor] Binary fractions, recommendations?

Liam Clarke ml.cyresse at gmail.com
Thu Aug 3 13:44:45 CEST 2006


Hi all,

I was recently playing with the problem of implementing the floor()
functionality, modulo being specifically mentioned in the briefing...

so, after grokking that x = a - (a % b) would do it (i.e. for a = 5.5
and b = 1, you'd get x =5) I felt very pleased...

...until I saw the definiton of modulo.

Which would be...

def my_mod(x, y):
	return x - (y*math.floor(x/y))

Ack. My floor() relies on modulo which relies on... floor.

So after some Googling, I find a page which indicates that the trick
seems to be bit operations  -
http://www.diycalculator.com/popup-m-round.shtml

Problem is; I can't bitshift on floats, so can't test. Can't bitshift
on floats in C either.  Does anyone know how I could work on a binary
representation of a float? Any language at all, I'm desperate...

Regards,

Liam Clarke


More information about the Tutor mailing list