|
Post by milfredo on Jun 11, 2020 23:22:22 GMT -5
The following code doesn't work. I wrote it like this because the Select Case statement wouldn't work either. I am taking a number like 47.36 and removing the 6 and then taking the 3 and adding 1 making it a 4. I tried first to use code W MOD 2 to check for odd number but that didn't work either. Could there be something wrong with my compiler? Even though W is one of the numbers the Debug program just skips through all the if statements without executing.
SeccallTime = secCalltime + secBtnLen
An = (Int(SeccallTime* 10) * .10)
W = ((An - (Int(An)))* 10)
if W = 1.0 then
An = W + 1
end if
if W = 3.0 then
An = W + 1
end if
if W = 5 then
An = W + 1
end if
if W = 7 then
An = W + 1
end if
if W = 9 then
An = W + 1
end if
test = W
|
|
|
Post by metro on Jun 12, 2020 0:20:03 GMT -5
not sure if this helps
Num= 47.36 print using("###.#",Num)
or
Num= 47.36 roundedNum$ = using("###.#",Num) print Val(roundedNum$)
|
|
|
Post by milfredo on Jun 12, 2020 0:31:43 GMT -5
Thanks but I'm not trying to print a number. Trying to assign a value to a Variable.
|
|
|
Post by metro on Jun 12, 2020 0:32:48 GMT -5
Thanks but I'm not trying to print a number. Trying to assign a value to a Variable. Yup Num= 47.36 roundedNum$ = using("###.#",Num) print Val(roundedNum$)
roundedNum$ is a variable
|
|
|
Post by milfredo on Jun 12, 2020 1:12:00 GMT -5
Oh. Ok. Thanks
|
|
|
Post by milfredo on Jun 12, 2020 1:22:22 GMT -5
I tried that and it works fine with two digits behind the decimal. But for a number like 49.1 it doesn't as I need to change the 1 into a 2. I need to change each 1, 3, 5, 7, 9 into 1 number higher when they are the third digit in a number such as 47.7
|
|
|
Post by Chris Iverson on Jun 12, 2020 1:41:57 GMT -5
Unfortunately, you're running headlong into a well-known issue with computers: they don't do decimals very well. The system most often used is called floating-point numbers, and the problem is, they can be inaccurate, and their accuracy gets worse sometimes, especially as multiplication and division enter the fray. What also doesn't help is LB's internal type coercion. In the comparison for W = 3.0, the hard-coded 3.0 is getting converted to an integer value "3", however, due to the previous floating-point operations on the value of W, W stays as a float. Because of weird floating-point math, 3.0 <> 3. You can try converting the number with int() before doing the comparison; int(W) = 3 should work fine. If you're always going to be working with two-decimal-digit numbers(or smaller), then I would recommend multiplying the initial value by 100, int()-ing it to get rid of any remaining floating-point value, and doing all the math as integer math, which computers are very good at. Sadly, this kind of data type seeming-nonsense is exactly what LB does it's best to shield you from, but it doesn't always work out. Here's a good example: the math I do should add up to 3.0, and the two values should be even. But if I pull out their exact hex values, you'll see that they don't match. SeccallTime = 47.36
An = (Int(SeccallTime* 10) * .10)
W = ((An - (Int(An)))* 10)
struct a, b as double a.b.struct = W
aFull$ = a.struct
struct c, d as double val = 2.5 a = 1 / 10 for y = 1 to 5 val = val + a next y
c.d.struct = val
cFull$ = c.struct
for x = len(aFull$) to 1 step -1 chr$ = mid$(aFull$, x, 1) print right$("00" + dechex$(asc(chr$)), 2); next x print
for x = len(cFull$) to 1 step -1 chr$ = mid$(cFull$, x, 1) print right$("00" + dechex$(asc(chr$)), 2); next x print
print a.b.struct print c.d.struct
if W = 1.0 then
An = W + 1
end if
if W = 3.0 then
An = W + 1
end if
if W = 5 then
An = W + 1
end if
if W = 7 then
An = W + 1
end if
if W = 9 then
An = W + 1
end if
test = W If you put those hex values into a converter online, you'll see that they're both close to 3.0, but they're not the same. Copy one of those long hex strings, and put it in the third box on this page: gregstoll.com/~gregstoll/floattohex/and hit "convert to double", and you'll see the number it represents. Do the same for the other number. The two different values I got were 3.0000000000000426 and 3.0000000000000004.
|
|
|
Post by metro on Jun 12, 2020 1:56:10 GMT -5
I wonder if this will do , a function I found some time back EDIT , Not ideal . should have tested more first sorry
for x = 49 to 50 step .01 print "x = "; x ; " rounded to "; round(x,0) myvariable = round(x,0) print myvariable next wait
function round(x,places) round = sgn(x)*int((abs(x)*10^(-1*places))+0.5)*(10^places) end function 'round function sgn(x) if x<0 then sgn = -1 else sgn = 1 end if end function 'sgn
|
|
|
Post by milfredo on Jun 12, 2020 19:03:14 GMT -5
Thank you guys very much. I'll see what I can do.
|
|