Recent

Author Topic: Noob help:  (Read 4510 times)

barrydocks

  • Newbie
  • Posts: 4
Noob help:
« on: July 13, 2013, 09:25:41 am »
So I am trying to learn Lazerus, I am working through Start Programming Using Object Pascal but i seem to have stumbled at one of the first hurdles: when I use var: Single as in this code:
Code: [Select]
var
x: Single;
begin
x:= 1.8;
Writeln('My Car engine capacity is ', x, ' liters');
Writeln('Press enter key to close');
Readln;
end.
the numbers are returned as:
Code: [Select]
My Car engine capacity is  1.799999952E+00 liters
Press enter key to close

I am using version 0.9.30.4-0 on Linux Mint 14

Any suggestions - please be gentle  :)

idc65

  • Jr. Member
  • **
  • Posts: 50
Re: Noob help:
« Reply #1 on: July 13, 2013, 10:01:36 am »
Hi

You could use the FloatToStrF function to format the output ie

Writeln('My Car engine capacity is '+FloatToStrF(x, ffFixed, 5, 3)+' liters');


the 5 is total length of formatted result, and the 3 is how many digits after the decimal place.

barrydocks

  • Newbie
  • Posts: 4
Re: Noob help:
« Reply #2 on: July 13, 2013, 10:13:05 am »
Thanks for the reply.

But what I Want to know is why the code didn't work as predicted?

Thanks

idc65

  • Jr. Member
  • **
  • Posts: 50
Re: Noob help:
« Reply #3 on: July 13, 2013, 10:39:37 am »
Floating-point accuracy loss can be the result of multiple causes:
If you assign a floating-point literal (for example: 0.1) to a floating-point variable, the floating-point variable might not have sufficient bits to hold the desired value without introducing some error. The floating-point literal might require a very large number or even an infinite number of bits for infinite precision representation.

More info can be found here, not all lazarus but the facts are the same.
a bit of light reading.
http://docwiki.embarcadero.com/RADStudio/XE4/en/Floating-Point_Rounding_Issues

Leledumbo

  • Hero Member
  • *****
  • Posts: 8797
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Noob help:
« Reply #4 on: July 13, 2013, 11:22:08 am »
When you round 1.799999952E+00 to 1 number after decimal separator, it's 1.8.

Rails

  • Guest
Re: Noob help:
« Reply #5 on: July 13, 2013, 01:50:20 pm »
Take a look at these:

http://en.wikipedia.org/wiki/Floating_point
http://floating-point-gui.de/

More than you ever wanted to know about floating point math and computers.   :D
« Last Edit: July 13, 2013, 01:55:18 pm by Rails »

barrydocks

  • Newbie
  • Posts: 4
Re: More Noob help:
« Reply #6 on: July 14, 2013, 08:53:43 pm »
Ok, Thanks.  Next question, why do I get this error:
Code: [Select]
project1.lpr(41,7) Error: Identifier not found "Format"
With this code:
Code: [Select]
var
Height: Double;
Weight: Double;
IdealWeight: Double;
begin
Write('What is your height in meters (e.g. 1.8 meter) : ');
Readln(Height);
Write('What is your weight in kilos : ');
Readln(Weight);
if Height >= 1.4 then
IdealWeight:= (Height - 1) * 100
else
IdealWeight:= Height * 20;if (Height < 0.4) or (Height > 2.5) or (Weight < 3) or
(Weight > 200) then
begin
Writeln('Invalid values');
Writeln('Please enter proper values');
end
else
if IdealWeight = Weight then
Writeln('Your weight is suitable')
else
if IdealWeight > Weight then
Writeln('You are under weight, you need more ',
Format('%.2f', [IdealWeight - Weight]), ' Kilos')
else
Writeln('You are over weight, you need to lose ',
Format('%.2f', [Weight - IdealWeight]), ' Kilos');
Write('Press enter key to close');
Readln;
end.

Thanks

eny

  • Hero Member
  • *****
  • Posts: 1646
Re: Noob help:
« Reply #7 on: July 14, 2013, 09:21:07 pm »
Ok, Thanks.  Next question, why do I get this error:
Because you can simply do:
Code: [Select]
Writeln('My Car engine capacity is ', x:5:2, ' liters');

(Or add 'sysutils' to your uses section...)
« Last Edit: July 14, 2013, 09:23:43 pm by eny »
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

barrydocks

  • Newbie
  • Posts: 4
Re: Noob help:
« Reply #8 on: July 14, 2013, 09:41:34 pm »
Ok, Thanks.  Next question, why do I get this error:
Because you can simply do:
Code: [Select]
Writeln('My Car engine capacity is ', x:5:2, ' liters');

(Or add 'sysutils' to your uses section...)
Ah-a, penny drops from a long way up!

Thanks

 

TinyPortal © 2005-2018