Crazy Math Conversion Question

buntay

Senior Member
Hello collective I'm back....hahaha

I have a question for the mathematicians among us. I need a multiplier of 3 but every 4th entry add 1.... hard to explain so here is the code. The only thing is the b1 numbers cant change from what I have listed in code.
Code:
setfreq m8

main:
count c.2,3000 ,b0
readtemp C.1, b2
readadc10 C.0,w2
b3=b2 *9 /5 + 32

if b0 < 4 then nevermind
if b0 = 4 then let b1= 13
if b0 = 5 then let b1= 16
if b0 = 6 then let b1= 20
if b0 = 7 then let b1= 23
if b0 = 8 then let b1= 26
if b0 = 9 then let b1= 30
if b0 = 10 then let b1= 33
if b0 = 11 then let b1= 36
if b0 = 12 then let b1= 40
if b0 = 13 then let b1= 43
if b0 = 14 then let b1= 46
if b0 = 15 then let b1= 50
if b0 = 16 then let b1= 53
if b0 = 17 then let b1= 56
if b0 = 18 then let b1= 60
if b0 = 19 then let b1= 63
if b0 = 20 then let b1= 66
if b0 = 21 then let b1= 70
if b0 = 22 then let b1= 73
if b0 = 23 then let b1= 76
if b0 = 24 then let b1= 80
if b0 = 25 then let b1= 83
if b0 = 26 then let b1= 86
if b0 = 27 then let b1= 90
if b0 = 28 then let b1= 93
if b0 = 29 then let b1= 96
if b0 = 30 then let b1= 100
if b0 = 31 then let b1= 103
if b0 = 32 then let b1= 106
if b0 = 33 then let b1= 110
if b0 = 34 then let b1= 113
if b0 = 35 then let b1= 116
if b0 = 36 then let b1= 120
if b0 = 37 then let b1= 123
if b0 = 38 then let b1= 126
if b0 = 39 then let b1= 130
if b0 = 40 then let b1= 133
if b0 = 41 then let b1= 136
if b0 = 42 then let b1= 140
if b0 = 43 then let b1= 143
if b0 = 44 then let b1= 146
if b0 = 45 then let b1= 150
if b0 = 46 then let b1= 153
if b0 = 47 then let b1= 156
if b0 = 48 then let b1= 160
if b0 = 49 then let b1= 163
if b0 = 50 then let b1= 166
if b0 = 51 then let b1= 170
if b0 = 52 then let b1= 173
if b0 = 53 then let b1= 176
if b0 = 54 then let b1= 180
if b0 = 55 then let b1= 183
if b0 = 56 then let b1= 186
if b0 = 57 then let b1= 190
if b0 = 58 then let b1= 193
if b0 = 59 then let b1= 196
if b0 = 60 then let b1= 200
if b0 = 61 then let b1= 203
if b0 = 62 then let b1= 206
if b0 = 63 then let b1= 210
if b0 = 64 then let b1= 213
if b0 = 65 then let b1= 216
if b0 = 66 then let b1= 220
if b0 = 67 then let b1= 223
if b0 = 68 then let b1= 226
if b0 = 69 then let b1= 230
if b0 = 70 then let b1= 233
if b0 = 71 then let b1= 236
if b0 = 72 then let b1= 240
if b0 = 73 then let b1= 243
if b0 = 74 then let b1= 246
if b0 = 75 then let b1= 250
if b0 = 76 then let b1= 253
if b0 = 77 then let b1= 256
if b0 = 78 then let b1= 260
if b0 = 79 then let b1= 263
if b0 = 80 then let b1= 266
if b0 = 81 then let b1= 270
if b0 = 82 then let b1= 273
if b0 = 83 then let b1= 276
if b0 = 84 then let b1= 280
if b0 = 85 then let b1= 283
if b0 = 86 then let b1= 286
if b0 = 87 then let b1= 290
if b0 = 88 then let b1= 293
if b0 = 89 then let b1= 296
if b0 = 90 then let b1= 300
if b0 = 91 then let b1= 303
if b0 = 92 then let b1= 306
if b0 = 93 then let b1= 310
if b0 = 94 then let b1= 313
if b0 = 95 then let b1= 316
if b0 = 96 then let b1= 320
if b0 = 97 then let b1= 323
if b0 = 98 then let b1= 326
if b0 = 99 then let b1= 330
if b0 = 100 then let b1=333


sertxd (#b1,",",#b3,",",#w2,cr,lf)

nevermind:
goto main

Any help would be much appreciated.

Side note: I did not use a word variable for count, because count will NEVER be higher than 100
 
Last edited:

hippy

Technical Support
Staff member
Can't guarantee this is right but something like this ...

b1 = (b0*3) + ( (b0 mod 4) Max 1 )

Or in PICAXE Basic ...

b1 = b0 Mod 4 Max 1
b1 = b0 * 3 + b1

But note you can't store anything greater than 255 in byte variables; eg b1 = 333
 
Last edited:

hippy

Technical Support
Staff member
I need a multiplier of 3 but every 4th entry add 1
Not quite according to your code. Take b0 multiply by 3, then look to see how many you need to add to get the b1 result ...

1 * 3 + 0 = 3
2 * 3 + 0 = 6
3 * 3 + 0 = 9
4 * 3 + 1 = 13
5 * 3 + 1 = 16
6 * 3 + 2 = 20
7 * 3 + 2 = 23
8 * 3 + 2 = 26
9 * 3 + 3 = 30
10 * 3 + 3 = 33
11 * 3 + 3 = 36

There are only two "+ 1" entries in there so something is amiss.
 

marks

Senior Member
Hi buntay,
try this maybe
Code:
setfreq m8

main:
count c.2,3000 ,w0
readtemp C.1, b2
readadc10 C.0,w2
b3=b2 *9 /5 +32

if w0 < 4 then main
w0=w0 *10 /3


sertxd (#w0,",",#b3,",",#w2,cr,lf)
 

Goeytex

Senior Member
Side note: I did not use a word variable for count, because count will NEVER be higher than 100
However, b1 WILL be greater than 100 and needs to be a WORD variable.

Here's a simple solution that does not require funny maths.
Code:
EEPROM 0,  (0,0,0,0,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9)
EEPROM 30, (10,10,10,11,11,11,12,12,12,13,13,13,14,14,14,15,15,15,16,16,16,17,17,17,18,18,18,19,19,19)
EEPROM 60, (20,20,20,21,21,21,22,22,22,23,23,23,24,24,24,25,25,25,26,26,26,27,27,27,28,28,28,29,29,29)
EEPROM 90, (30,30,30,31,31,31,32,32,32,33,33)

setfreq m8

Symbol addend = b10

main:
  count c.2,3000 ,b0
  readtemp C.1, b2
  readadc10 C.0,w2
  b3 = b2 * 9 / 5 + 32

Read b0, addend
w3 = b0 * 3 + addend 

sertxd (#w3,",",#b3,",",#w2,cr,lf)

goto main
 

westaust55

Moderator
Taking into account the "anomoly" mentioned by hippy, this will otherwise work

w1 = b0 /3 : w1 = b0*3 + b1 ; using w1 (not b1) to allow for results > 255
 

Axel87

Senior Member
Just wanted to chime in on this one.
Can anyone expand on Marks formula, as far as what the code is doing?
For us not-so-brilliant guys out there?
 

BeanieBots

Moderator
*9 /5 +32 looks very much like Celcius to Farenheight conversion.
(the +1's probably being to take into account rounding errors)
 

westaust55

Moderator
Just wanted to chime in on this one.
Can anyone expand on Marks formula, as far as what the code is doing?
For us not-so-brilliant guys out there?
If you think about the solution that I provided,
First I calculated one-third of b0 and then added that to b0 * 3 to get the final answer is in effect multiplying by 3 and 1/3

Marks more elegant solution uses b0 * 10 / 3
10 / 3 = 3 and 1/3

Both rely upon the integer math giving only the whole number for each group of three.
 
Top