Skip to content

Bug with Point coordinates in scientific notation #514

Closed

Description

@Tradioyes

This bug is solved by the resolution of issue #437 in v3.0, but I did not see this bug reported before.

It is still present in (at least) v2.2.x, and causes a crash.

When a point coordinate is lower than 0.001ish (I don't know the exact point), there is an automatic switch to the scientific notation by Postgres. The insertion goes well, but the read from the db fails with a FormatException in ExtendedBackendToNativeTypeConverter.

Test to reproduce it (in CommandTests):

[Test]
public void TestPointScientificSupport()
{
    ExecuteNonQuery("INSERT INTO data (field_point) VALUES ( '(4, 0.00003)' )");
    var command = new NpgsqlCommand("select field_point from data", Conn);
    var p = (NpgsqlPoint)command.ExecuteScalar();
    Assert.AreEqual(4, p.X);
    Assert.AreEqual(0.00003, p.Y);
}

StackTrace:

System.InvalidCastException : Le format de la chaîne d'entrée est incorrect.
  ----> System.FormatException : Le format de la chaîne d'entrée est incorrect.
   à Npgsql.ForwardsOnlyDataReader.GetProviderSpecificValue(Int32 ordinal) dans NpgsqlDataReader.cs: line 1443
   à Npgsql.ForwardsOnlyDataReader.GetValue(Int32 Index) dans NpgsqlDataReader.cs: line 1416
   à Npgsql.NpgsqlCommand.ExecuteScalar() dans NpgsqlCommand.PrepareExecute.cs: line 420
   à NpgsqlTests.CommandTests.TestPointScientificSupport() dans CommandTests.cs: line 1718
--FormatException
   à System.Number.ParseSingle(String value, NumberStyles options, NumberFormatInfo numfmt)
   à System.Single.Parse(String s, NumberStyles style, IFormatProvider provider)
   à NpgsqlTypes.ExtendedBackendToNativeTypeConverter.ToPoint(NpgsqlBackendTypeInfo TypeInfo, Byte[] bBackendData, Int16 TypeSize, Int32 TypeModifier) dans NpgsqlTypeConvBackendToNative.cs: line 341
   à NpgsqlTypes.NpgsqlBackendTypeInfo.ConvertBackendTextToNative(Byte[] BackendData, Int16 TypeSize, Int32 TypeModifier) dans NpgsqlTypeInfoBackend.cs: line 210
   à NpgsqlTypes.NpgsqlTypesHelper.ConvertBackendStringToSystemType(NpgsqlBackendTypeInfo TypeInfo, Byte[] data, Int16 typeSize, Int32 typeModifier) dans NpgsqlTypesHelper.cs: line 248
   à Npgsql.StringRowReader.ReadNext() dans NpgsqlAsciiRow.cs: line 84

Again, this is solved by #437, so is a bugfix in 2.2.x doable?

Activity

  1. roji commented on Mar 2, 2015

    @roji
    Member

    This seems to be a matter of fixing the regular expressions in NpgsqlTypeConvBackendToNative, I'll do this for 2.2.5.

  2. added this to the 2.2.5 milestone on Mar 2, 2015
  3. self-assigned this
    on Mar 2, 2015
  4. roji commented on Mar 2, 2015

    @roji
    Member

    I've pushed a fix for this in 2.2, it's now building on the build server and will be released with 2.2.5.

    However, please note that in 2.2 NpgsqlPoint is mistakenly parsed as a pair of Single values (it should be double, fixed in 3.0). Also, your assertion for complete equality to 0.00003 is too strict - floating point values should be compared with a tolerance value.

  5. added a commit that references this issue on Mar 2, 2015
    0098911
  6. Tradioyes commented on Mar 2, 2015

    @Tradioyes
    ContributorAuthor

    Sorry for the Equality comparison, I just did a quick test to show the stacktrace by editing the existing TestPoint.
    I'll edit my version to support doubles until 3.0 comes out, thanks for the patch!

  7. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions