Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Count digits after point (https://www.delphipraxis.net/179120-count-digits-after-point.html)

WojTec 15. Feb 2014 15:10

Delphi-Version: 2010

Count digits after point
 
I need to know how many digits has float number after point.

Delphi-Quellcode:
function CountDigitsAfterPoint(AValue: Extended): Byte;
begin
  Result := 0;

  while (Frac(AValue) <> 0) do
  begin
    Inc(Result);
    AValue := AValue * 10;
  end;
end;
Working good in most cases, but not for all :o

Incorect results for sample data:
2.048 --> 19
8.448 --> 19
565.148 --> 17
32.064 --> 18
274.176 --> 17
97.728 --> 17
155.52 --> 17
622.08 --> 17

:shock:

Please help :(

Aphton 15. Feb 2014 15:29

AW: Count digits after point
 
That is because there are some inaccuracy regarding non-integer numbers.
0.99999999999999999999.... is equal to 1!
(or your example: 2.048 could internally be 2.04799999999999999...)
Therefore your method needs to take that into account.
But sadly I have no idea since my knowledge about precision is not good. Someone else might be of help!

Edit: Another problem that came to mind is irrational numbers. I thought Id mention it but it shouldnt be too problematic since the data-type cuts off after a certain degree of precision..

DelphiProgrammer 15. Feb 2014 15:32

AW: Count digits after point
 
Hi,
maybe that'll help:
Code:
decimalplaces := (length(floattostr(number)) - pos(',',floattostr(number))-1);

Aphton 15. Feb 2014 15:41

AW: Count digits after point
 
@DelphiProgrammer:
Nope!

Delphi-Quellcode:
var
  x: Single;

begin
  x := 2.048;
  writeln(x);
  writeln((length(floattostr(x)) - pos('.',floattostr(x))-1));
  readln;
end.
Output
Code:
 2.04800009727478E+0000
15

WojTec 15. Feb 2014 15:51

Re: Count digits after point
 
I thought about strings too, but (1) it's lame and (2) see @Aphton's post.

There is no math solution? :(

Aphton 15. Feb 2014 16:07

AW: Count digits after point
 
This might be a problem that can be solved in another way.
You want to find out the number of fraction-digits.
Why?
Maybe the root problem you are trying to solve can be solved in another way! Maybe you dont even need to find out the digitcount!

mkinzler 15. Feb 2014 16:09

AW: Count digits after point
 
Why you need the number of digits? perhaps there is a better way to achieve your aim.

Aphton 15. Feb 2014 16:10

AW: Count digits after point
 
@mkinzler: xDD

WojTec 15. Feb 2014 16:50

Re: Count digits after point
 
So, reason is not important. Important is problem.
But if you want to know: (1) result is required for further computation purposes and (2) for formating. :)

mkinzler 15. Feb 2014 16:56

AW: Count digits after point
 
For (1) because the system iminent computation error the consumption more digits are better doesn't seem right to me.
For (2) I'd prefer to restrict the digits to dispay to a fixed count

swkevin08 15. Feb 2014 16:59

AW: Count digits after point
 
as the others have said, it has something to do with the approach.
but look at this. maybe it will help you!

ps: doenst work for all numbers! for example x,42300000000000000001

Edit: e was randomly generated

Code:
const
  e = 0.0000000001;

function CountDigitsAfterPoint(AValue: Extended): Byte;
var r: Extended;
begin
  Result := 0;
  r := Int(AValue);
  while ((Frac(AValue) <> 0) and ((AValue - r) > e)) do
  begin
    Inc(Result);
    AValue := AValue * 10;
    r := Int(AValue+e);
  end;
end;

WojTec 15. Feb 2014 17:49

Re: Count digits after point
 
Thanks, for all values I used early, is working correctly.
BTW: why e has that value, can have other values?

swkevin08 15. Feb 2014 18:32

AW: Count digits after point
 
e is just a small number close to zero. it doesn't matter if there's one zero more or one less.

Sir Rufo 15. Feb 2014 20:53

AW: Re: Count digits after point
 
Zitat:

Zitat von WojTec (Beitrag 1248005)
Thanks, for all values I used early, is working correctly.
BTW: why e has that value, can have other values?

Have a look at the documentation Delphi-Referenz durchsuchenSystem.Math.SameValue and Delphi-Referenz durchsuchenSystem.Math.CompareValue and read what is the meaning of Epsilon.

Another helpful article is http://en.wikipedia.org/wiki/Machine_epsilon

Furtbichler 15. Feb 2014 21:07

AW: Re: Count digits after point
 
Zitat:

Zitat von WojTec (Beitrag 1247995)
So, reason is not important. Important is problem.
But if you want to know: (1) result is required for further computation purposes and (2) for formating. :)

The reason is always important, especially when people don't seem to exactly know what they are talking about.

Showing the number of decimals is definitely not something to please the eye. It is showing the accuracy of your value, so a value of, say, 100 (metres) measured up to a mm is correctly shown as "100,000 m" whereas the same value with an accuracy of 1m is shown as "100m".


Alle Zeitangaben in WEZ +1. Es ist jetzt 20:04 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz