Recent

Author Topic: Math Question  (Read 2008 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Math Question
« on: March 24, 2019, 01:12:58 am »
Var
 IntValue : Integer = 119;
 PercentValue : Real = 0.04587987;
 Answer : Integer = 0;

 Can't do the following:

  Answer := IntValue * Percent;

Do I have to convert everything to real to do this math?

Would like to end up with Answer =  5. 
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Math Question
« Reply #1 on: March 24, 2019, 01:27:55 am »
@ Do I have to convert everything to real to do this math?

Yes. Or use function to convert floating point value to integer: round() or trunc()
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Math Question
« Reply #2 on: March 24, 2019, 01:29:15 am »
The operation:
Code: [Select]
IntValue * Percentresults on a Real number, which can't be assigned to an Integer. Do it like this:
Code: Pascal  [Select][+][-]
  1. Answer := Round(IntValue * PercentValue);

ETA: I'm too slow :-[ Yeah, what Blaazen said.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: Math Question
« Reply #3 on: March 24, 2019, 01:30:23 am »
Var
 IntValue : Integer = 119;
 PercentValue : Real = 0.04587987;
 Answer : Integer = 0;

 Can't do the following:

  Answer := IntValue * Percent;

Do I have to convert everything to real to do this math?

Would like to end up with Answer =  5.

Round
https://www.freepascal.org/docs-html/rtl/system/round.html

Int
https://www.freepascal.org/docs-html/rtl/system/int.html
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Math Question
« Reply #4 on: March 24, 2019, 03:24:38 am »
@dsiders

I did look at both of those URL's you posted and tried a few things, but kept getting an compiler error.

The ---> Answer := Round(IntValue * PercentValue); worked.

For some reason I thought everything had to be real or Integer but they couldn't be mixed within the equation.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Math Question
« Reply #5 on: March 24, 2019, 04:39:50 am »
Do I have to convert everything to real to do this math?

No — of course, you can multiply the integer by float and you don't need any conversion, because the integer will be automatically converted to float before multiplication. The problem is only that the result of such a multiplication will be float, which cannot be assigned to an integer without conversion.

Therefore, either use a function that converts a floating point result to an integer (such as Round), or overwrite the operator.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

 

TinyPortal © 2005-2018