Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Wont Let Me Divide By Sum Made By Arrays


  • Please log in to reply
2 replies to this topic

#1 HelpMePlz


    CC Newcomer

  • Member
  • PipPip
  • 20 posts

Posted 26 April 2012 - 11:53 AM

I have a total which I have arrived at by using the following codes:

For j := 0 to 100 do begin
                                  temp3 := (UsersShares[j] * Week1SharePrices[j]);
                                  PortWorthWeek1 := PortWorthWeek1 + temp3;

For some reason lazarus won't let me divide something by PortWorthWeek1.

Any ideas why? It's not a problem with the division or anything as it works with everything else but for some reason it wont let me divide anything bythis number.

This is the code I am using to try and divide using PortWorthWeek1

PortPerChange := (((PortWorth - PortWorthWeek1) * 100) / PortWorth);

But the program crashes when it trys to run this piece of code.

Thanks for the help!

The funny thing is that it works for pretty much the exact same code when used with arrays:

PercentageChange := (((SharePrices[j] - Week1SharePrices[j]) * 100) / SharePrices[j]);

That runs just fine.
  • 0

#2 WingedPanther73


    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 26 April 2012 - 02:56 PM

Where are your declarations? It's almost impossible to know what's going on with so little code.

Also, are you getting a compile-time or run-time error?

Finally, where do you set PortWorth? It will default to 0, a no-no for dividing.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

#3 Luthfi


    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 26 April 2012 - 07:26 PM

In this case, it's helpful to know that types were the involved variables. Every types, especially numeric, has range limitation that your code might had violated. Please post their declarations.

Anyway, you can try to analyze the problem by placing breakpoint on this line:
PortPerChange := (((PortWorth - PortWorthWeek1) * 100) / PortWorth);

then run the program. When the program is paused on the breakpoint, you can check the value of each variable then find out if the math calculation is okay (no divide by zero) or possibility of range violation.
  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download