Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   integer division (http://www.chiefdelphi.com/forums/showthread.php?t=38905)

ImmortalAres 11-07-2005 14:35

integer division
 
the following is part of kevins accelerometer code


Code:


#include "p18f452.h"
#include "accel.h"

unsigned int X_Axis_Total_Period = 2/1000; // x-axis total cycle time (T2 in the data sheet)
unsigned int X_Axis_Pulse_Period = 0; // x-axis amount of time the pulse is high (T1 in the data sheet)
unsigned int Y_Axis_Total_Period = 2/1000; // y-axis total cycle time (T2 in the data sheet)
unsigned int Y_Axis_Pulse_Period = 0; // y-axis amount of time the pulse is high (T1 in the data sheet)


my debugger says it loops in a file called 16 bit integer division leading me to think that it hangs on the 2/1000 definition of t2

any ideas?

Mike Betts 11-07-2005 15:01

Re: integer division
 
Quote:

Originally Posted by ImmortalAres
the following is part of kevins accelerometer code


Code:


#include "p18f452.h"
#include "accel.h"

unsigned int X_Axis_Total_Period = 2/1000; // x-axis total cycle time (T2 in the data sheet)
unsigned int X_Axis_Pulse_Period = 0; // x-axis amount of time the pulse is high (T1 in the data sheet)
unsigned int Y_Axis_Total_Period = 2/1000; // y-axis total cycle time (T2 in the data sheet)
unsigned int Y_Axis_Pulse_Period = 0; // y-axis amount of time the pulse is high (T1 in the data sheet)


my debugger says it loops in a file called 16 bit integer division leading me to think that it hangs on the 2/1000 definition of t2

any ideas?

Integer division: 2/1000 is equal to 0

In fact, 999/1000 = 0 unless your math routine does rounding...

ImmortalAres 11-07-2005 16:12

Re: integer division
 
lovely

how do the programmers in FIRST usually get around things like that

(how do they express not whole numbers)

Kevin Watson 11-07-2005 17:00

Re: integer division
 
Quote:

Originally Posted by ImmortalAres
the following is part of kevins accelerometer code

Umm, you're wrong. In my original code I initialize X_Axis_Total_Period and Y_Axis_Total_Period to zero.

-Kevin

sciguy125 11-07-2005 20:54

Re: integer division
 
Quote:

Originally Posted by ImmortalAres
lovely

how do the programmers in FIRST usually get around things like that

(how do they express not whole numbers)

The easiest way would be to use floating point numbers. The more efficient way (because floating point takes more processor instructions) would be to multiply everything by some factor of 10. ie, your 2/1000 would be 2 if you multiplied everything that uses that number by 1000.

Kevin Watson 11-07-2005 21:19

Re: integer division
 
Quote:

Originally Posted by sciguy125
The more efficient way (because floating point takes more processor instructions) would be to multiply everything by some factor of 10. ie, your 2/1000 would be 2 if you multiplied everything that uses that number by 1000.

This is precisely how the code works. Those variables aren't in units of seconds, but in timer ticks. Each tick counts as 100 ns of elapsed time.

-Kevin


All times are GMT -5. The time now is 04:41.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi