[erlang-questions] KATT multiplies floats by a factor of 10 or 100.

Leonard B leonard.boyce@REDACTED
Thu Jan 18 14:56:26 CET 2018


Hello Bjanre,

If I'm looking at the correct source for KATT
(https://github.com/for-GET/katt), it looks like it may be a bug in
katt_util:my_float_to_list/2

Quick test.

1> F = fun My_float_to_list(X, Decimals) ->
1>   Multiplier = trunc(math:pow(10, Decimals)),
1>   X1 = X * Multiplier,
1>   X2 = trunc(X1) + 0.0,
1>   case X1 =:= X2 of
1>     true ->
1>       String = integer_to_list(trunc(X1)),
1>       Number = [ string:sub_string(String, 1, Decimals)
1>                , string:sub_string(String, min( Decimals + 1
1>                                               , string:len(String) + 1
1>                                               ))
1>                ],
1>       string:join(Number, ".");
1>     false ->
1>       Decimals1 = Decimals + 1,
1>       My_float_to_list(X, Decimals1)
1>   end end.
#Fun<erl_eval.36.54118792>
2> F(1.23,0).
"12.3"

On Thu, Jan 18, 2018 at 2:51 AM, Bjarne Wichmann Bagge Petersen
<bp@REDACTED> wrote:
> Have this head scratcher where I apply a float to a KATT parameter but in
> the request that float have been inflated by a factor of 10 or 100.
>
> I am wondering whether I am missing something obvious about how to treat
> floats in katt/jsx/erlang? Or this is a KATT bug?
>
> Got this minimal test case:
>
> float_test(Config) ->
>     File = filename:join([?config(data_dir, Config), "float_test.apib"]),
>     Params = [ {test_float, 1.23}],
>     katt:run(File, Params).
>
> And the contents of float_test.apib:
>
> POST http://localhost:9999
>> Accept: application/json
>> Content-Type: application/json
>> User-Agent: KATT
> {
>     "test": "{{<test_float}}",
>     "reference": 1.23
> }
> < 200
> < Content-Type: application/json
> {}
>
> And this is how the request looks using nc -l 9999
>
> POST / HTTP/1.1
> Accept: application/json
> Content-Type: application/json
> User-Agent: KATT
> Host: localhost:9999
> Content-Length: 43
>
> {
>     "test": 12.3,
>     "reference": 1.23
> }
>
> As you can see "test_float" have been multiplied by a factor 10.
>
>
> Best regards
>
>
> Bjarne Wichmann Bagge Petersen
>
> Backend Developer, ShopGun
>
>
> E: bp@REDACTED
> L: dk.linkedin.com/in/bjarnewp
> W: shopgun.com
> A: Arne Jacobsens Allé 16, Field's, 3rd floor, DK-2300 København S, Denmark
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list