Multiplikation mit Picaxe

elektriker

New Member
Hello Experts!
I would have to solve the following problem with PICAXE:

× * 0.996513 =

for example: × = 1766.66

Does anyone have a solution?
**
Thank you
 

westaust55

Moderator
What accuracy do you require?

As a start, you could multitask multiply the 0.9965 by say 100 or 1000

Then you are multiplying 996 by 1766

Low-word = 996 * 1766
High-word = 996 ** 1766

The answer is then high-word * 65536 + low word

How you use that rest may depend on what you wish to do with the result.
You certainly will not get multiple decimal places of accuracy.
I have done such math in the past to extract time and date from a 4/5 byte binary counter
But do not have access to the code over iPhone.

I believe Jeremy Leach posted some code for 32-bit math in the past. Try a forum search for that thread.

Other options may include use of a floating point unit such as the MicroMega uM-FPU V2 or V3.1.
 
Last edited:

AllyCat

Senior Member
Hi,

Westy's iPhone autocorrect has produced a few mistakes; I think "multitask" should read "multiply".

As he says, the method of handling decimals is to multiply by an integer, usually a power of 10. However, your 0.996513 is a potential problem (if you want to maintain full accuracy) because it would become a value of more than 16 bits. If one multiplier is always very close to one (like your example), you might multiply by (.00)3487 and then subtract the result from 1000000. Note that although maths within the PICaxe is limited to 16 bits, the Programme Editor can work with constants up to 31 bits (yes 31 not 32).

Indeed, Jeremy has shown some very clever maths (particularly his combined multiplication-division routine), but for some "brute force" 32-bit PICaxe maths, see my posts #2 and #1 here.

Cheers, Alan.
 

geoff07

Senior Member
It depends on what else is going on and the form in which you need the output, but you could do it by mimicing the manual method of long multiplication that would put each digit into it's own byte (or nibble if you used bcd). So:
Code:
   176666 x 10^-2
x  996513 x 10^-6
-------------------
  1569994     (x 9)
+  1569994    (x 9)
                         
+ etc (x 6,5,1,3))
-------------------
total         (x 10^-8)
Long-winded but it might be enough for your problem.
 

crowland

Member
1766 * 10000 will overflow a 16 bit integer.

The trick with this is to look at the problem and work out how to convert the floating point numbers into fixed point numbers that give a result to the required accuracy without overflowing the 16 bit limit.
One possibility is to multiply 1766 by 32, 0.966 by 2048 and use the ** command to get the high word of a 32 bit multiply. Nothing overflows
Code:
w0 = 56533		'1766.66 * 32
w1 = 1979		'0.966513 * 2048

w2 = w1 ** w0
Chris
 

westaust55

Moderator
But with x = 1766 the first math step * 10,000
Results in 17,660,000 which exceeds 65535 (internal math is at 16-bits) and thus there is overflow and consequent error.
 

AllyCat

Senior Member
Hi,

Surely the point is that the two "example" figures quoted by the OP each use more than 16 significant bits, disregarding the scaling.

If he really requires that resolution (which I personally doubt) then the method described by geoff (or an external hardware number cruncher) might be the way to proceed. But it's a significant programming task, almost certainly needing a good understanding of indirect addressing (@bptr, @ptr, PEEK, POKE, etc.) and likely to run very slowly on a PICaxe.

So we need the OP to come back to explain exactly the range and accuracy of the numbers that he actually needs to process.

EDIT: Ah, I've just remembered this thread in the code snippets section (but it's rather more than just a snippet).

Cheers, Alan.
 
Last edited:

marks

Senior Member
Hi elektriker,
welcome to the forum
as AllyCat points out it makes it a lot easier to know the range and accuracy required
heres one example Result scaled up by 10.
Code:
; using a High word (**65308) is ruffly the same as multiplying 0.99652 
   SYMBOL Interger         = W0       
   SYMBOL Decimal          = W1
   SYMBOL Result           = W2  
   

Interger = 1766  :  Decimal  = 66


 Result = Interger *10 **65308  
 Result = Decimal **65308 /10 + Result

Interger = Result  /10
Decimal  = Result //10
SERTXD (cr,lf,#Interger,".",#Decimal)
 

MartinM57

Moderator
@elektriker

Tell us more about the precision and number ranges you think you want/need.

You've made two posts on the forum now, both concerning mathematics that the PICAXE isn't really the best solution for, with no feedback from you at all - as you can see, people are more than willing to give you their free time, but some feedback and direction you want to go will help you, and the rest of us here

Until you do, IMHO, there's nothing more to add to this thread...
 

AllyCat

Senior Member
Hi,

I believe Jeremy Leach posted some code for 32-bit math in the past. Try a forum search for that thread.
It depends if the OP actually found Jeremy's code, because it appears to do almost exactly what he asked for (at least up to 16 significant bits, ~ 5 decimal digits). If so, then all the posts after #2 have been rather a waste of our time.

However, the forum search failed for me (even though I had a fair idea what I was looking for), but an external Google search immediately found them in #1 of this recent thread. Fairly compact and well-documented, a little heavy on variables (but now seems to be well within the capabilities of all M2s). However, still quite advanced stuff, perhaps not for the novice! ;)

Cheers, Alan.
 

oracacle

Senior Member
it may also help to know how you plan to use the result, if you just plan on displaying it or somehting more. the reason i ask is if you were to go the FPU route it chnages how you get the information back from the extra hardware you have to hook up.

there are benfits for either on PICAXE in code or sending it out to an FPU
 

elektriker

New Member
I'm home again.
For the posts I would like to thank the experts.

I learned a lot about integer math.



thank you very much
Werner

Sorry for my bad English with Google Translator.
 

westaust55

Moderator
Do you now have a solution to your math problem?

If not, you will need to answer some of the questions raised about accuracy and input value range.
 
Top