| |

VerySource

 Forgot password?
 Register
Search
View: 2428|Reply: 12

Simple and weird question about accuracy! !! !! Everyone help!

[Copy link]

1

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-3-11 10:30:01
| Show all posts |Read mode
var
  a: double;
  s: string;
begin
  a: = 8.07;
  s: = formatfloat ('#. ######################', a);
  showmessage (s);
end;

Everyone guess what the result is: 8.07000000000000028

why? Why is Delphi so strange?
Reply

Use magic Report

1

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-6-3 21:45:01
| Show all posts
Why is there another 28 behind it????

It seems that this is the case in any environment, and a relatively fixed number
Reply

Use magic Report

0

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-6-4 19:15:01
| Show all posts
Because its type is Double
Reply

Use magic Report

1

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-6-5 18:45:01
| Show all posts
But I assigned it directly. Why does the system force 000000000000028? Is there any good way to solve this problem?
Reply

Use magic Report

1

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-6-7 12:45:01
| Show all posts
Once I use ADOConnection.Open
a := 8.07000000000000028;

s := formatfloat('#.####################',a);

The s display is still 8.07, why?
Reply

Use magic Report

0

Threads

34

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-6-16 17:15:01
| Show all posts
Once I use ADOConnection.Open
a := 8.07000000000000028;
-----------------------------------------------
a := 8.07000000000000028; what is the relationship with using ADOConnection.Open? The landlord did not say.

Assuming that a is extracted from the data table, please ask the landlord to see how accurate the field is.
Reply

Use magic Report

1

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-6-25 12:30:01
| Show all posts
a := 8.07;
  s := formatfloat('#.#####################',a);
  showmessage(s);//Show 8.00700000000000028
  ADOConnection.Open
  a := 8.07;
  s := formatfloat('#.#####################',a);
  showmessage(s);//Show 8.07
Reply

Use magic Report

0

Threads

34

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-7-5 02:30:01
| Show all posts
On 32-bit Windows systems:

Double type: Representation range: ±5.0 * 10^-324 ~ 1.7 * 10^308   effective digits: 15~16 digits. So, change your formatFloat like this, try:
var
a: double;
s:string;
begin
  a := 8.07;
  s := formatfloat('#.##############',a);//Note that the decimal point is 15#
  edit1.text := s ;//Display 8.07

  s := format('%1.15f',[a]);
  edit2.text := s; //Display 8.070000000000000  It is recommended to use format()
end;

Use format() conversion, even if you find the adoconnection problem, it no longer exists.
Your problem, the first is that your precision is beyond the scope of double.
Reply

Use magic Report

0

Threads

34

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-7-5 07:00:01
| Show all posts
Oh, about the number of digits, I may be wrong. But using format() is correct.
Reply

Use magic Report

1

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-7-25 14:15:01
| Show all posts
var
  a:double;
  s:string;
  vADOConnection:Variant;
begin
  a := 8.07;
  s := formatfloat('#.################',a);//s display 8.07000000000000028
  vADOConnection := CreateOleObject('ADODB.Connection');
  vADOConnection.ConnectionString :='Driver={Firebird/InterBase(r) driver};pwd=masterkey;Uid=SYSDBA;database=C:\123.GDB';
  try
    vADOConnection.Open;
  Except
  end;
  a := 8.07;
  s := formatfloat('#.################',a);//s display 8.07
end;


How to achieve the above is 8.07000000000000028? ? ? ? ? ?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list