division question

golfnut

Member
Not great on this, hence the post..


Ive a factor to be held permanently in code and its 1333333.33

this forms the numerator every time I do a division conversion on a counter result.
A valid conversion may be :-

1333333.33 / 4092 hopefully would give 325.8 -I would like to display 326 on a 7segment!!
or
1333333.33 / 1332 or 1333 whatever,,,

24 bits for a numerator ??? also I dont like the look of the .33 on the end what would happen there??

GN
 

golfnut

Member
Hi, no its this years project!

1333333.33 (dec) divided by nos in range 1330 to 4100 (dec)

I thought computers were good at math ??
 

hippy

Ex-Staff (retired)
A rewrite of code earlier today for 32-bit divided by 16-bit ...

Code:
#Picaxe 20X2
#No_Table
#No_data
#Terminal 9600

Symbol K       = 13333333
Symbol Kmsw    = K / $00010000
Symbol Klsw    = K & $0000FFFF

Symbol Ndiv    = w10
Symbol Nmsw    = w11
Symbol Nlsw    = w12
Symbol Nresult = w13

For Ndiv = 1330 To 4100

  SetFreq M64

  Nresult = 0
  Nmsw    = Kmsw
  Nlsw    = Klsw

  Do while Nmsw <> 0 Or Nlsw >= Ndiv
    If Nlsw < Ndiv Then
      Nmsw = Nmsw - 1
    End If
    Nlsw = Nlsw - Ndiv
    Nresult = Nresult + 1
  Loop

  BinToAscii Nresult, b1,b2,b3,b4,b5

  SetFreq M8

  SerTxd( "1333333.33 / ", #Ndiv, " ~= ", b1,b2,b3,b4,".",b5, CR, LF )

Next
 

Jeremy Leach

Senior Member
Hmmm, I think the fact that 4000000/3 = 1333333.33 has got to be a little significant here :)

I'd work with 4000000 and see if you can think of a way to simplify the division.
 

Jeremy Leach

Senior Member
And another idea, would be to take this graph, and approximate the curve by a set of straight lines, which could be used to get the result, by feeding in the Denominator value. i.e a lookup with interpolation. Not the easiest thing to code but definitely doable.
 

Attachments

golfnut

Member
Hi thx for the code

Hi, I run your code on sim for an hour or so and kind got in a mess,

I added a value to w10 of 4100 to simulate having a count from other HW



Symbol Ndiv = w10
Symbol Nmsw = w11
Symbol Nlsw = w12
Symbol Nresult = w13

let w10 = 4100


For Ndiv = 1330 To 4100

but it would not print the result as it didnt come out of the loop, after a few thousand loops.
Now I now your the dude on this stuff , so it aint you.
So cmon where and how bad did I mess up..

GN
 

hippy

Ex-Staff (retired)
It will run slowly and 'take forever' as it's a repeated subtraction but it appears to be working when I tried simulating. Perhaps post the code you've changed things to.
 

marks

Senior Member
Hi Golfnut,
try this too lol
Code:
w10=4092   '     number 1330 to 4100

w11=w10*3/10
w12=40000/w11*10

w14=40000//w11*10/w11
w15=40000//w11*10//w11
w15=w15*10/w11 :if w15>5 then  let w14= w14+1 :endif
w10=w12+w14

BINTOASCII w10,b8,b7,b6,b5,b4 

SERTXD (CR, LF, "",b9,b8,b7,b6,b5,b4,"  ", CR, LF)
 

golfnut

Member
Hi Marks I tested your work and most of its great.

But some numbers dont work right.

e,g, 1333333/ count

if I enter denominator as count of 4040 the result is = 330 ok
if I enter denominator as count of 4050 the result is = 349 --- and it should be 329 ?

cheers
GN
 

golfnut

Member
Hippy, Hi.

with you code I cut and pasted it.

Added

Symbol Nresult = w13

let Ndiv = 4032 'This part'

For Ndiv = 1330 To 4100

And I took the # off the ndiv on the serial out line..

I cant get it to exit the loop.
GN
 

golfnut

Member
I caught a top gear episode and it had finished

Display red 1333333.33 1333333.33 13333333.33 .......

Im redoing it with the # back in

GN
 

hippy

Ex-Staff (retired)
with you code I cut and pasted it.

Added

Symbol Nresult = w13

let Ndiv = 4032 'This part'

For Ndiv = 1330 To 4100

And I took the # off the ndiv on the serial out line..

I cant get it to exit the loop.
Putting an "Ndiv=" before "For Ndiv=" will have absolutely no effect, the FOR will set Ndiv immediately overwriting the previous assignment. I've no idea why you took the # out; it is there for a reason :)

Using the simulator it appears that the variables are changing as I would expect but I didn't let it finish as I got bored waiting for it to complete. There will be numerous iterations before the loop does exit; 10,000 iterations for each result.

Say simulation does 10 iterations of the loop per second, 10,000 iterations will take 1,000 seconds; over 16 minutes. Longer if you don't have simulation running at the fastest speed. If you ran the entire program through simulation it would take over a month to complete.

Some things aren't suited to simulation, and this is one of them. That's also the reason the 20X2 is set to run at 64MHz; division by repeated subtraction can be incredibly slow especially with large enumerators and small divisors.
 

golfnut

Member
Perhaps I worded my conundrom wrong from the outset,,

I will have a count -- I dont know what it will be , , but I know that the range of values will be approx what was stated 1332 to 4100 ish.

I will do a measure - somehow and get a count val as above - say 4000.

where shall I place my count number?

this Number has to get into the PICAXE and the code you kindly provided will pick it up and process it to give a result

i would be mega pleased if 4000 gave result output of 333

How long may this take on the real uprocessor - a few seconds max?

thanks again - for your support, time and effort. It is appreciated and it does turn projects I would have had to abandon into tangible learning kit..
GN
 
Last edited:

marks

Senior Member
Hi Golfnut,
can you please recheck your formula or maybe your reusing a variable somewhere.
I simulated 4050 with this program and it did return 329.
thanks marks
Code:
w10=4040'     number 1330 to 4100

w11=w10*3/10
w12=40000/w11*10

w14=40000//w11*10/w11
w15=40000//w11*10//w11
w15=w15*10/w11 :if w15>5 then  let w14= w14+1 :endif
w10=w12+w14

BINTOASCII w10,b8,b7,b6,b5,b4 
SERTXD (CR, LF, "",b9,b8,b7,b6,b5,b4,"  ", CR, LF)
after thinking about why you got a correct result for 4040 that is the value of w12(basic accuracy) ie w14 would be 0. (so you have a problem with adding w14 ).
info w14 improves accuracy of the count
w15 is the decimal part ie if > than 5(0.5) then 1 will be added to 14
hope this helps a bit lol.

for 4040 w12=330 w14=0 w15=0 ie (330.0)
for 4050 w12=320 w14=9 w15=2 ie(329.2)
 
Last edited:

hippy

Ex-Staff (retired)
This will calculate a single value; gives 1333333.33 / 4032 ~= 0330.6 on a real chip ...

Code:
#Picaxe 20X2
#No_Table
#No_data
#Terminal 9600

Symbol K       = 13333333
Symbol Kmsw    = K / $00010000
Symbol Klsw    = K & $0000FFFF

Symbol Ndiv    = w10
Symbol Nmsw    = w11
Symbol Nlsw    = w12
Symbol Nresult = w13

Ndiv = 4032

SetFreq M64

Nresult = 0
Nmsw    = Kmsw
Nlsw    = Klsw

Do while Nmsw <> 0 Or Nlsw >= Ndiv
  If Nlsw < Ndiv Then
    Nmsw = Nmsw - 1
  End If
  Nlsw = Nlsw - Ndiv
  Nresult = Nresult + 1
Loop

BinToAscii Nresult, b1,b2,b3,b4,b5

SetFreq M8

SerTxd( "1333333.33 / ", #Ndiv, " ~= ", b1,b2,b3,b4,".",b5, CR, LF )
 

golfnut

Member
Hi Marks I ran your code again.

4050 comes out as ..

00349 on th serial output buffer window

maybe its a setting I dont have on my sim?

I have trouble displaying the simulated lcd too.
thanks for the code

hope you got the weather its 'toilet' here

GN
 

golfnut

Member
code in HW

Thanks Hippy, takes about 3 seconds to do the math.

I gotta make a wombai counter and some opto devices

Then gotta work out how to get my counter 3byte wide No into picaxe!

thanks again..
 

Attachments

Top