Skip to content

Saving "Infinity" in a Float column #651

Open

Description

@neongrau

Noticed that when i tried to divide a string number which i converted to_f and then used as a divider, when the float is 0.0 ruby returns Infinity instead of a divison by zero erro. Which then the Adapter tries to persist like this:

e.g.

value = "0"
record.some_float = 1000 / value.to_f
ActiveRecord::StatementInvalid: TinyTds::Error: Error converting data type nvarchar to float.
...
@0 = Infinity

Is this correct/intended behavior of the adapter?

This sure was only a bug in my apps code but i wondered why this ended up in the SQL.

Activity

  1. metaskills commented on Jun 11, 2018

    @metaskills
    Member

    Interesting, what version of the adapter/rails? Also, can you tell me more about the schema (column type) under the hood for some_float?

  2. neongrau commented on Jun 11, 2018

    @neongrau
    Author

    just a plain FLOAT(8) that came from a migration what was simply created by
    t.float :some_float

    i googled a bit and it seems that while other DBs support NaN, INF and -INF like the IEEE standard describes.
    See https://www.codeproject.com/Tips/50340/MSSQL-doesn-t-completely-support-IEEE-floating

    So i guess nothing reasonable could be done on the adapter part. Probably best to see the error like it now happens.

  3. neongrau commented on Jun 11, 2018

    @neongrau
    Author

    and my adapter version is the current 5.1.6

  4. metaskills commented on Jun 11, 2018

    @metaskills
    Member

    I do think this is an adapter but tho and we can improve the logic to rase an error when assigning. Here is our Float type.

    https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/5-1-stable/lib/active_record/connection_adapters/sqlserver/type/float.rb

    And here is the one we inherit from in ActiveModel.

    https://github.com/rails/rails/blob/5-1-stable/activemodel/lib/active_model/type/float.rb

    I think we should handle the Float::INFINITY differently than the base model and I'd be surprised if we were not the first adapter to do so.

  5. thomasfedb commented on Dec 3, 2018

    @thomasfedb

    Might be best for the Type::Float to error out somehow before the statement is executed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions