Why do I get this result ?

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

Why do I get this result ?

Post 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.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Why do I get this result ?

Post 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?
- Don Kinzer
FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

Post 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
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post 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.
- Don Kinzer
FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

Post 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 ?
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post 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.
- Don Kinzer
FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

Post 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
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post 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.
- Don Kinzer
Post Reply