werid problem

Just starting out? Need help? Post your questions and find answers here.
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

werid problem

Post by Dreglor »

Hello, i'am new to purebasic and to the fourms and i been playing around with pb and i found that some lines of code don't work in some places
inparticular, lines that return to a varibal

i have to find one because i found a work around to them

but it just annoying becuase it will return one number like it was a constant it can only set once and then never change but it does not give any errors what so ever

if any one has found this problem before please tell me if this is a bug or something i doing wrong
~Dreglor
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Lets see some code! :-)
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

Post by Dreglor »

sorry it took so long but i found it
i had forgoten what i got it from
i try it again it it does it work
but if i use a diffrent (but identical resulting) line of code it works perfectly
i have tested this in a diffrent lang and it works just fine...

heres the code:

Code: Select all

If InitSprite()=0
  MessageRequester("Fatal Error!","Could not initalize directX",0)
  End
EndIf
#screen_width.w=1024
#screen_height.w=768
If OpenScreen(#screen_width,#screen_height,16,"")=0
  MessageRequester("Fatal Error!","Could not initalize the screen",0)
  End
EndIf
If InitKeyboard()=0
  MessageRequester("Fatal Error!","Could not initalize the keyboard",0)
  End
EndIf
SetFrameRate(60) ;i put this in to keep the frame rate constant
xmax.w=10000
ymax.w=10000
zmax.w=2000
sspeed.w=-10
zmin.w=10
num.l=5000 ;get slow around 55000-60000 and i have 2.66 cpu ( by slow i mean if it gets below 60 fps, the set frame rate)
centerx.w=#screen_width/2
centery.w=#screen_height/2
zoom.w=60
shade.w=0
fnum.w=0
fps.s="0"
Dim sx(num)
Dim sy(num)
Dim sz(num)
For i=0 To num
    sx(i)=Random(xmax)-xmax/2
    sy(i)=Random(ymax)-ymax/2
    sz(i)=Random(zmax)
Next i
Repeat
  StartDrawing(ScreenOutput())
  start=Date()
  For i=0 To num
    sz(i)=sz(i)+sspeed
    If sz(i)<=zmin
      sz(i)=zmax
      sx(i)=Random(xmax)-xmax/2
      sy(i)=Random(ymax)-ymax/2
    EndIf
    screenx.w=(sx(i)*zoom)/sz(i)+centerx
    screeny.w=(-sy(i)*zoom)/sz(i)+centery
    shade=255-(sz(i)/zmax*255) ;does'nt work always returns 255 i don't know why
    ;shade=Int(255/zmax* -sz(i)) ;does work
    If screenx < #screen_width
      If screeny < #screen_height
        If screenx > 0
          If screeny > 0
           Plot(screenx,screeny,RGB(shade,shade,shade))
          EndIf
        EndIf
      EndIf
    EndIf
  Next i
  DrawText("FPS: "+fps)
  StopDrawing()
  FlipBuffers()
  ClearScreen(0,0,0)
  ExamineKeyboard()
  If Date() > start
    fps=Str(fnum)
    fnum=0
    start=Date()
  Else
    fnum=fnum+1
  EndIf
Until KeyboardReleased(#PB_Key_Escape)

CloseScreen()
End
the one that doesn't work only out puts 255
and can any one verify that my fps counter work properly?

so if any one spot why this isn't working i would love to know why it happing and if it should do that
~Dreglor
MikeB
Enthusiast
Enthusiast
Posts: 183
Joined: Sun Apr 27, 2003 8:39 pm
Location: Cornwall UK

Post by MikeB »

In the line that does not work you are taking a number sz(i) which is up to
2000 and dividing by 2000 so that the result is always 0. something.
PB is then interpreting this as an integer and so the result is always 0.
0 * anything is still 0
so you get 255-0 = 255 every time!

simple really

MikeB
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

To summarise, all you have to do to fix the original line is change line 25 to

Code: Select all

shade.f=0 
Alternatively you can wrap the bad line in an Int(), just like the good line.

Interestingly, if you remove the Int() from the good line, it now fails but this time the result is always 0, so you see nothing unless you change the background colour.

If you are assigning to a variable then that target seems to be used for storing interim results. This probably saves having to allocate new memory locations so it could be a deliberate optimisation.

When the expression is destined for a function call, then that calls paramater is the target paramater.

Here's a demonstration:

Code: Select all

Procedure.l lParam(p.l)
  ProcedureReturn p * 100
EndProcedure

Procedure.f fParam(p.f)
  ProcedureReturn p * 100
EndProcedure

l.l = (10/3) * 100
f.f = (10/3) * 100

Debug l
Debug f
Debug lParam(10/3)
Debug fParam(10/3)
returns

Code: Select all

300
333.333313
300
333.333313
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

Post by Dreglor »

hmm

i think the problems comes as a result of data types because the bad line it has 2 diffrent data types (word and long) so maybe purebsic does not have any datatype converting yet? or is it just buggy?

from what i can see wrapping int() around the line thanks GedB
~Dreglor
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

so maybe purebsic does not have any datatype converting yet?
You mean like Javascript? No no no... thats just lazy! :D PureBasic is a 'Statically Typed' language, like all professional languages are. You must define a type for each variable and stick to it (meaning: the variable can not change type during run time).
--Kale

Image
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

Yes, Purebasic is optimised for Speed.

Other languages trade processor time for the programmers convenience.
Post Reply