Page 1 of 1

Why do I get this result ?

Posted: 21 March 2010, 12:32 PM
by FFMan

Code: Select all

Sub Main()
  dim sn_work as single
  dim by_MMfn as byte
	
  sn_work=30.00001
  by_MMfn=cbyte(sn_Work / 60.0)
  debug.print cstr(by_mmfn)
End Sub
Result is 1 for any value of sn_work > 30

Either I'm very tired or there is something unexpected happening here and my display clock function doesn't work as a result.

Re: Why do I get this result ?

Posted: 21 March 2010, 14:06 PM
by dkinzer
FFMan wrote:[T]here is something unexpected happening here [...].
The result is exactly as described in the documentation for CByte(). What result were you expecting?

Posted: 21 March 2010, 14:41 PM
by FFMan
I see - i thought cbyte was just a type conversion, but it is rounding also, hence my problem.

would this produce the best way to get the desired result

by_MMfn=cbyte(fix(sn_Work / 60.0))

i wrongly though my first version of code was equivlent to other forms of basic that do not require explicit type conversion. Would i be correct in saying

by_MMfn=30.0001/60.0 in VB6 would result in zero ?

thanks

Posted: 21 March 2010, 15:07 PM
by dkinzer
FFMan wrote:Would i be correct in saying by_MMfn=30.0001/60.0 in VB6 would result in zero ?
No. CByte() in VB6 exhibits the same behavior.

If you want conversion without rounding, see the description of FixB(). Another option is to use Fix() or Floor() to get the integral portion of the Single value and then convert that to Byte using CByte(). The difference between Fix() and Floor() is how negative values are handled, for non-negative values the results are the same.

Posted: 21 March 2010, 15:31 PM
by FFMan
Don

OK i see - I guess i never used cbyte in VB as type conversion is implied.

However I tried using Fixb and I get a result I don't understand. The code lines are:-

debug.print cstr(sn_work)
debug.print "Min=";cstr(fixb(sn_work/60.0));" floor=";cstr(floor(sn_work/60.0))

and the debug output when the clock goes over 30 secs:-

30.00391
Min=1 floor=0.0

It does work if use cbyte(floor(sn_work/60.0)) but as ever trying to use as few commands as possible I thought fixb on its own should work ?

Where have I gone wrong ?

Posted: 21 March 2010, 16:36 PM
by dkinzer
FFMan wrote:Where have I gone wrong ?
You haven't. Rather, you've found an error in the code generation for FixB() for native mode devices. It works as described on VM mode devices.

Posted: 22 March 2010, 1:50 AM
by FFMan
ok thanks don

This explains why i didn't see the problem in testing as i was on a ZX24a.

Will you issue a fix or shall I code using Floor instead ?

thanks

Posted: 22 March 2010, 8:05 AM
by dkinzer
FFMan wrote:Will you issue a fix or shall I code using Floor instead ?
The correction has been made and will be in a near future release of the compiler. In the interim, you'll need to use Floor() or Fix() within a CByte() call to get the desired result.