Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Other Forums > Math and Physics


Math and Physics Discuss the complex nature of mathmatics and physics relating to electronic circuitry.

Reply
 
Thread Tools Display Modes
Old 27th March 2008, 12:29 AM   (permalink)
Experienced Member
 
Blog Entries: 4
blueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to behold
Send a message via Skype™ to blueroomelectronics
Default Divide by 6 using C and avoiding floating point?

I'm doing a dice program using a freerunning timer that automatically and fairly counts from 0-6^x (where x is number of die)
Now one die is easy (I'm a BASIC programmer but want to do it in C18)

So
A single die is roll+1

Two dice are D1=int(roll/6)+1 and D2=(roll mod 6)+1

Any ideas on 3 or more dice? And avoiding floating point...
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is online now   Reply With Quote
Old 27th March 2008, 01:25 AM   (permalink)
Experienced Member
Pommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to behold
Default

Isn't it just

D1=roll mod 6
D2=(roll/6) mod 6
D3=(roll/36) mod 6
D4=(roll/216) mod 6
etc.

If you do it in integer maths then it should just work.

Mike.
Pommie is offline   Reply With Quote
Old 27th March 2008, 03:20 AM   (permalink)
Experienced Member
 
Blog Entries: 4
blueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to behold
Send a message via Skype™ to blueroomelectronics
Default

There you go, thanks I just couldn't see the forest for the trees.
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is online now   Reply With Quote
Old 27th March 2008, 11:44 AM   (permalink)
Experienced Member
 
Hero999 is a splendid one to beholdHero999 is a splendid one to beholdHero999 is a splendid one to beholdHero999 is a splendid one to beholdHero999 is a splendid one to beholdHero999 is a splendid one to beholdHero999 is a splendid one to behold
Default

Does the MCU you're using have a hardware divide instruction or are you useing a maths library?

The x86 does and puts the quotent and remainder in to different registers. Using a high level language like C will probably end up producing two divide instructions, one to get the remainder and another for the quotent which is a wate of clock cycles. I'd recommend doing the division part in assembler, that way you can use one instruction to get both the quotent and remainder.
__________________
What's so bad about Microsoft?

Get Opera it's simply a superb browser.
Hero999 is offline   Reply With Quote
Old 27th March 2008, 02:11 PM   (permalink)
Experienced Member
Pommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to behold
Default

Quote:
Originally Posted by Hero999
Does the MCU you're using have a hardware divide instruction or are you useing a maths library?

The x86 does and puts the quotent and remainder in to different registers. Using a high level language like C will probably end up producing two divide instructions, one to get the remainder and another for the quotent which is a wate of clock cycles. I'd recommend doing the division part in assembler, that way you can use one instruction to get both the quotent and remainder.
What use is the remainder? For D4 the calculation goes, int(roll/216), result/6 and keep remainder. Two divides required anyway. D2 is the only one that could benefit from the remainder but the additional code wouldn't be worth it.

Mike.
Pommie is offline   Reply With Quote
Old 27th March 2008, 04:52 PM   (permalink)
Experienced Member
 
Hero999 is a splendid one to beholdHero999 is a splendid one to beholdHero999 is a splendid one to beholdHero999 is a splendid one to beholdHero999 is a splendid one to beholdHero999 is a splendid one to beholdHero999 is a splendid one to behold
Default

I don't see why more than one divider per iteration is required.

Roll is the variable with the the value generated by the timer.

Divide, is the divide instruction, it accepts two variables, the first value is divided by the second value. After the instruction is executed the quotent is stored in the first variable and the remainder is stored in the second.

e.g.

x = 105
y = 10
Divide x, y
Print x, y

The output will be:
10 5

Assuming the PIC has a similar instruction, here's how you'd use it. Roll is the random number which is set before the code runs.

Do
Remainder = 6
Divide Roll, Remainder
Print Remainder
Loop until Roll = 0
__________________
What's so bad about Microsoft?

Get Opera it's simply a superb browser.
Hero999 is offline   Reply With Quote
Old 27th March 2008, 09:13 PM   (permalink)
Experienced Member
 
Blog Entries: 4
blueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to behold
Send a message via Skype™ to blueroomelectronics
Default

Well the formula should work well as an unsigned int function (now I'll have to learn C18 functions)
Code:
for Roll = 0 to 6^4-1
D1 = int(Roll/1 mod 6)+1
D2 = int(Roll/6 mod 6)+1
D3 = int(Roll/36 mod 6)+1
D4 = int(Roll/216 mod 6)+1
print Roll, D1, D2, D3, D4
next
Just BASIC program
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is online now   Reply With Quote
Old 27th March 2008, 11:05 PM   (permalink)
Experienced Member
Pommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to behold
Default

Hero,
I see what you mean now. Doing it in asm would save a divide.

Bill,
In C it would simply be,
Code:
    D2=((Roll/6)%6)+1;
Not much difference really.

Mike.
Pommie is offline   Reply With Quote
Old 27th March 2008, 11:16 PM   (permalink)
Experienced Member
 
Blog Entries: 4
blueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to behold
Send a message via Skype™ to blueroomelectronics
Default

Thanks I always thought % meant binary. But in C it means modulo.

Using special mode the CCP allows TMR1 to be a free running modulo x counter.
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com

Last edited by blueroomelectronics; 27th March 2008 at 11:20 PM.
blueroomelectronics is online now   Reply With Quote
Old 27th March 2008, 11:45 PM   (permalink)
3v0
Moderator
 
Blog Entries: 3
3v0 is a splendid one to behold3v0 is a splendid one to behold3v0 is a splendid one to behold3v0 is a splendid one to behold3v0 is a splendid one to behold3v0 is a splendid one to behold3v0 is a splendid one to behold
Default

0xAA = 0b10101010
__________________
search engine for electronic parts
Junebug USB PIC programmer kit. USB Bit Wacker
3v0 is online now   Reply With Quote
Old 27th March 2008, 11:51 PM   (permalink)
Experienced Member
Pommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to beholdPommie is a splendid one to behold
Default

Quote:
Originally Posted by blueroomelectronics
Using special mode the CCP allows TMR1 to be a free running modulo x counter.
Ahh, the special event trigger, one of my favourites.

Mike.
Pommie is offline   Reply With Quote
Old 28th March 2008, 01:37 AM   (permalink)
Experienced Member
 
Blog Entries: 4
blueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to behold
Send a message via Skype™ to blueroomelectronics
Default

Quote:
Originally Posted by Pommie
Ahh, the special event trigger, one of my favourites.

Mike.
It's a real gem, it was one of your older posts that pointed it out to me.

It occurred to me to let it free run till you press a button, presto instant random number.
I could do the software in Swordfish easy, but it's more a challenge to do it in C18.

Eventually I'll make a Risk game battle dice (5 dice) roller on the Junebug as one of the tutorials.
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is online now   Reply With Quote
Old 8th April 2008, 06:25 AM   (permalink)
Experienced Member
petrv will become famous soon enough
Default

You can of course use the compiler's runtime library to do the division if the PIC does not have a hardware divider, but if it is just for 2 dice why don't you use repeated substraction in a loop ? Stupid ,yes but very simple and performance is not a concern in this case, anyway the number of iterations would be 0 to 5 so who cares ? This will give you both the result of the division and the remainder... easy to do even in assembly.
Petr
petrv is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes




All times are GMT. The time now is 11:17 PM.


Electronic Circuits  |  Radio Controlled
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.