weird bug (fixed)

General FreeBASIC programming questions.
Post Reply
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

weird bug (fixed)

Post by dafhi »

Code: Select all

'' commenting out line 15 hides the bug
'' loop at the bottom (For i as long .. ) also seems to expose it

#define max( a, b)        iif( (a)>(b), (a), (b) )

function palette_generalist( i as long, c as short = 8 ) as ulong
  
  var u = max(c - 1, 1)
  var gray_count = cint( log(sqr(c))/log(2) ) + 1
  
  var in = i/ u
  var partitions = max(gray_count - 1, 1)
  var part_offs      = (max(gray_count - 1, 1) / u)
  
  if i = u then ? part_offs
  
  var _pos = in*partitions
  
  #define _sawtooth_base  _pos
  var _saw = _sawtooth_base - int(_sawtooth_base)
  if i = u then
   ?
   ? "_pos";_pos
   ? "int(_pos)"; int(_pos)
  endif
  
  
  return -1
  #undef _sawtooth_base
end function


var pal_size = 50, u = pal_size-1

for i as long = 0 to 49 '' also has something to do w/ loop
  var col = palette_generalist( i, pal_size )
Next
Last edited by dafhi on May 18, 2023 13:34, edited 1 time in total.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: weird bug

Post by srvaldez »

dafhi, what's the expected output/behavior ?
btw, I am not fond of misusing var, it may take a bit more typing but it's worth for code clarity, besides it takes out the guesswork
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: weird bug

Post by dafhi »

it should be 3. i see 2

i'll take your suggestion into consideration
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: weird bug

Post by srvaldez »

dafhi, I get 3 in both 32 and 64 with the latest FB, what version are you using?
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: weird bug

Post by dafhi »

1.09. i'll try new

[edit] still see 2
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: weird bug

Post by paul doe »

Code: Select all

'' commenting out line 15 hides the bug
'' loop at the bottom (For i as long .. ) also seems to expose it

#define max( a, b)        iif( (a)>(b), (a), (b) )

function palette_generalist( i as long, c as short = 8 ) as ulong
  var u = max(c - 1, 1)
  
  var gray_count = cint( log(sqr(c))/log(2) ) + 1
  
  var in = i/ u

  var partitions = max(gray_count - 1, 1)
  var part_offs      = (max(gray_count - 1, 1) \ u) '' Use integer division here
  
  if i = u then ? part_offs
  
  var _pos = in*partitions
  
  #define _sawtooth_base  _pos
  var _saw = _sawtooth_base - int(_sawtooth_base)
  if i = u then
   ?
   ? "_pos";_pos
   ? "int(_pos)"; int(_pos)
  endif
  
  
  return -1
  #undef _sawtooth_base
end function

var pal_size = 50, u = pal_size-1

for i as long = 0 to 49 '' also has something to do w/ loop
  var col = palette_generalist( i, pal_size )
Next

sleep()
As srvaldez points out, this is no bug.

@dafhi: indeed, as you can see, using var along with the definition of max introduced bugs into your code. Never let the compiler guess the type for you on computations; saving a few keystrokes is not worth wasting your time with needless debugging.
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: weird bug (fixed)

Post by dafhi »

this fixed it

Code: Select all

  dim as single in = i/ u
thanks
Post Reply