Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversion of double to string can result in a loss of precision #408

@btasker

Description

@btasker

When the client starts building LP, it converts double and float values to a string:

                if (value is double || value is float)
                {
                    sb.Append(((IConvertible)value).ToString(CultureInfo.InvariantCulture));
                }

However, CultureInfo.InvariantCulture may not preserve full precision.

Whereas, if that's replaced with "R" the precision will be maintained:

For example

using System;
using System.Globalization;
					
public class Program
{
	public static void Main()
	{
		double a = (double) 459.29587181322927;
		
		string b = a.ToString(CultureInfo.InvariantCulture);
		Console.WriteLine(b);
		
		string c = a.ToString("R");
		Console.WriteLine(c);
		
	}
}

Generates

459.295871813229
459.29587181322927

However, this isn't a full solution, because the use of R may also add unexpected precision. If we redefine a to be 45.29587181323112 in the example above, then we get 45.295871813231123 because of the way that C# handles conversion.

Steps to reproduce:
List the minimal actions needed to reproduce the behavior.

  1. Use the client
  2. Define a double with >15 significant numbers
  3. Write into InfluxDB

Expected behavior:
The full precision should be sent

Actual behavior:
The value will have lost precision

Specifications:

  • Client Version:
  • InfluxDB Version: 1.x/2.x
  • Platform: 4.7.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions