0

My contract .Cpp part

void mainloan::adduwr(name acc_name, double interest, uint64_t loan_amount )
{
  print("Adding underwriter", acc_name);
  // require_auth( _self );

  uwr_table.emplace(get_self(), [&](auto &u) {
    u.acc_name = acc_name;
    u.interest_rate =interest;
    // u.loan_amount=loan_amount;
    u.giving_loan_amunt = loan_amount;
  });
}

.hpp part

 struct [[eosio::table]] underwriter_info
  {
    name acc_name;
    // std::vector<uint64_t> interest_rate;
    // std::vector<uint64_t> loan_amount; 
      double interest_rate;
      uint64_t giving_loan_amunt;
    auto primary_key() const
    {
      return acc_name.value;
    }
  };

  typedef eosio::multi_index<"underwriter"_n, underwriter_info> underwriter;

  underwriter uwr_table;


  [[eosio::action]] 
  void adduwr(name acc_name,double interest, uint64_t loan_amount);

Cleos call

guru@ubuntu:~/contracts/mainloan$ cleos --url http://jungle2.cryptolions.io:80 push action microfinanc1 adduwr ['lender',8.6,10000] -p microfinanc1
executed transaction: 26b8218bbcd623430b440efecfcbb395cc5430f25d16900497e96c006c78977f  120 bytes  210 us
#  microfinanc1 <= microfinanc1::adduwr         {"acc_name":"lender","interest":"8.59999999999999964","loan_amount":10000}
>> Adding underwriterlender
warning: transaction executed locally, but may not be confirmed by the network yet         ] 

so the problem happens when i called the action

cleos --url http://jungle2.cryptolions.io:80 push action microfinanc1 adduwr ['lender',8.6,10000] -p microfinanc1

output from cleos

{"acc_name":"lender","interest":"8.59999999999999964","loan_amount":10000}

so 8.6 is chnaged to 8.59999999999999964 why this is happening? if anyone have any suggestion to give,please say it.

1

The builtin floating-point types store values as finite binary fractions. For technical specification, see Wikipedia. For a human-readable explanation of the why, a good starting point is The Floating-Point Guide.

The number 8.6 is not representable as a finite binary fraction. So, the double type stores the closest possible approximation instead, which is displayed to you as 8.59999999999999964.

| improve this answer | |
  • so what should i do for this,and this thing never happened with me till yesterday,actually yesterday i tried doing string to float conversion for the same code,instead of taking double rate argument i was taking string and then converting it into float,i had to do this due to some problem in float types in flutter .So i tried conversion & my data precision got reduced as it looks now,but i wanted precised data thats why i removed that code,but some how that code is still giving same output.Never faced this issue before – guruprakash gupta Nov 26 '19 at 6:01
  • and also Even from flutter App tried to call the same action, and it gave the error saying String is not type double for interest_rate & then i type cast it to double then it worked but 0.000001 got reduced @gassa please reply if u get these comments – guruprakash gupta Nov 26 '19 at 6:13
  • @guruprakashgupta Naturally, if you want to use floating point, and got in trouble doing so, you should learn to better use floating point. Perhaps the links in this answer are a good start. – Gassa Nov 26 '19 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.