odd division in functions

Miscellaneous Forums/General Discussion/odd division in functions

hmmm... probably a bad discription for the title...
sorry bout that. Anywho, I finally got a computer again... so I sat down and started writting a quick lil software renderer (textureless for now... I haven't learnt that stuff yet). So I was writting the "draw polygon" function... and for some wierd reason they weren't rendering right... to my suprise it was because the simple division used to calculate the slope was rounding off... then I noticed I just really couldn't get any divisions to work in a function without rounding off... is this to do with the version I'm using? I haven't got the internet on right now... so I'd hafta download it here (from TAFE)
but has anyone else ever had this problem?
it's really anoying... I never noticed it before...
and it really halted my software renderer development.

What was the function supposed to be returning?

I assume you have b3d, as you havent said, and I also assume that b3d returns INTs as default, so you have to make sure it is returning a float

oh... sorry about that...
I'll have to look into that...
I've been a bit out of it...
I'm using blitz3d.
the function was pretty much a rendering command...
like: Drawpoly(x0,y0,x1,y1,x1,y1)
it doesn't really return anything,
it just draws the polygon.

Then I noticed I just really couldn't get any divisions to work in a function without rounding off... is this to do with the version I'm using? I haven't got the internet on right now... so I'd hafta download it here (from TAFE)
but has anyone else ever had this problem?


It's a bit of a vague description -- when you say "rounding off", do you mean up/down to the nearest integer? or does it gives you some decimals, just not the ones you were expecting?

It might be helpful to post a sample division here, listing the values you are feeding it and the results you are getting as well as the results you were expecting...

without that info, it's unlikely anyone can figure out what the problem is just by your initial post above.

The problem most probably lies in the int -> float conversion. To avoid rounding, use Floor or Ceil.

Make sure all the variables you need to have fractional bits are floats (ie put a # after the variable name), and also note that when you do division with a constant number, you have to write it with a decimal place after to make sure the division is done as a floating point number, ie:
2/x will round off if x is an int
but
2.0/x will give a floating point number.

... I feel really embarrassed now...
I got it working...
yup... I just needed to use the float command...
thanks guys.

It's terrible when you start coding after a long break...you make all the sme dumb mistakes you'd sharpened upto before. I'm there now sigh...but the good thing is you wise up quicker than before!