Bitcoin Forum
August 17, 2024, 10:27:44 PM *
News: Latest Bitcoin Core release: 27.1 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Why you can't use "0.1" in bitcoin code safely  (Read 736 times)
earlz (OP)
Sr. Member
****
Offline Offline

Activity: 439
Merit: 250


mmmmmm


View Profile WWW
August 05, 2014, 09:35:56 PM
 #1

I just wanted to give a quick FYI to all the coin devs and future coin devs.

When you use "0.2*COIN" to represent 0.2 coins, it is not safe. Floating point values in C++ are not guaranteed to be exact, and they are not guaranteed to be consistent across platforms or even compilers. Please stop doing this. For more information, see this stackoverflow question.

The proper way to represent a division of coins is to either represent it as satoshis (ie, without COIN), or to use only integers, such as "2*COIN/10" to represent 0.2 coins.

This WILL cause forks and bad wallets on non-x86 architectures, such as the raspberry pi and many others and is in general a very bad practice.

Check out my full list of Coin Reviews
Currently working on the Qtum Project
My Skype account earlzdotnet has been compromised. Please report to me any usage seen from it, or from the email earlz@earlz.biz.tm
coinsolidation
Sr. Member
****
Offline Offline

Activity: 294
Merit: 250

Bitmark Developer


View Profile WWW
August 05, 2014, 09:51:03 PM
 #2

Why not create a small unit test application and host it on github, something which tests that the values expected are produced.

Link us to it, and post it in the technical section, many people will run it, if or when it fails we can then take some action.

Bitmark (reputation+money) : Bitmark v0.9.4 (release)
r3wt
Hero Member
*****
Offline Offline

Activity: 686
Merit: 504


always the student, never the master.


View Profile
August 05, 2014, 09:55:21 PM
 #3

We're not doing any math with the block reward code, other than the initial <value> * COIN, so one might argue, the issue of block halving would become an issue far sooner than the block reward, since there could be feasibly some precision loss and desynchronization accross different platforms once the formula moves from <value> * COIN to <value>/2 * COIN;

there are probably safeguards in place to prevent this anyway, probably thought of by the original creator of bitcoin. Also, remember bitcoin units are only required to be precise to 8 decimal places, leaving a rather large margin of error.

My negative trust rating is reflective of a personal vendetta by someone on default trust.
earlz (OP)
Sr. Member
****
Offline Offline

Activity: 439
Merit: 250


mmmmmm


View Profile WWW
August 06, 2014, 04:12:41 AM
 #4

We're not doing any math with the block reward code, other than the initial <value> * COIN, so one might argue, the issue of block halving would become an issue far sooner than the block reward, since there could be feasibly some precision loss and desynchronization accross different platforms once the formula moves from <value> * COIN to <value>/2 * COIN;

there are probably safeguards in place to prevent this anyway, probably thought of by the original creator of bitcoin. Also, remember bitcoin units are only required to be precise to 8 decimal places, leaving a rather large margin of error.

No, you're misunderstanding how the code actually works. In the code, there is no nothing but satoshis. "1" in the native unit of bitcoin, is a satoshi. COIN is defined as 10*8. All units in bitcoin are stored and conveyed using a 64bit integer, not a floating point number. There is a reason for this, and there is a reason that no protocol critical code uses floating point numbers. Programmers have heard of things like 0.1+0.1 = 0.19999999901 and such related horrors, well imagine that for the block reward, but it's also not consistent. So, the wallet on your raspberry pi thinks that the block reward should be 0.1999172. and the wallet on your x86 PC, thinks it should be 0.199999999998. and the wallet on your Android device using ARM thinks it should be 0.19998.

Check out my full list of Coin Reviews
Currently working on the Qtum Project
My Skype account earlzdotnet has been compromised. Please report to me any usage seen from it, or from the email earlz@earlz.biz.tm
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!