Browse Source

add decode_binary clause for floats with value 0.0

Float decoding failed when Value was 0.0 because math:log10(0.0) throws
an exception

* add clause handling the issue
* add test for the issue

resolves #27
derwinlu 9 years ago
parent
commit
3023252070
2 changed files with 4 additions and 1 deletions
  1. 3 0
      src/mysql_protocol.erl
  2. 1 1
      test/mysql_tests.erl

+ 3 - 0
src/mysql_protocol.erl

@@ -627,6 +627,9 @@ decode_binary(#col{type = T, decimals = S, length = L}, Data)
 decode_binary(#col{type = ?TYPE_DOUBLE},
               <<Value:64/float-little, Rest/binary>>) ->
     {Value, Rest};
+decode_binary(#col{type = ?TYPE_FLOAT}, <<0.0:32/float-little, Rest/binary>>) ->
+    %% TYPE_FLOAT conversation fails on math:log10(0.0)
+    {0.0, Rest};
 decode_binary(#col{type = ?TYPE_FLOAT},
               <<Value:32/float-little, Rest/binary>>) ->
     %% There is a precision loss when storing and fetching a 32-bit float.

+ 1 - 1
test/mysql_tests.erl

@@ -293,7 +293,7 @@ float_rounding(Pid) ->
     {ok, Select} = mysql:prepare(Pid, "SELECT f FROM f"),
 
     %% [{Input, Expected}]
-    TestData = [{1.0, 1.0}, {3.14, 3.14}, {0.2, 0.2},
+    TestData = [{1.0, 1.0}, {0.0, 0.0}, {3.14, 3.14}, {0.2, 0.2},
                 {0.20082111, 0.200821}, {0.000123456789, 0.000123457},
                 {33.3333333, 33.3333}, {-33.2233443322, -33.2233},
                 {400.0123, 400.012}, {1000.1234, 1000.12},