Bitcoin Forum
May 08, 2024, 06:23:14 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: « 1 [2]  All
  Print  
Author Topic: Developers - Best practises for decimal handling  (Read 3213 times)
Seal (OP)
Donator
Hero Member
*
Offline Offline

Activity: 848
Merit: 1078


View Profile WWW
February 14, 2013, 12:23:13 AM
 #21


Any decimal number can be exactly represented this way.


Unless you are using javascript which treats decimals as floats  Huh

DefiDive - Filter the noise
A clean crypto asset management terminal
No Gods or Kings. Only Bitcoin
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715192594
Hero Member
*
Offline Offline

Posts: 1715192594

View Profile Personal Message (Offline)

Ignore
1715192594
Reply with quote  #2

1715192594
Report to moderator
1715192594
Hero Member
*
Offline Offline

Posts: 1715192594

View Profile Personal Message (Offline)

Ignore
1715192594
Reply with quote  #2

1715192594
Report to moderator
Xenland
Legendary
*
Offline Offline

Activity: 980
Merit: 1003


I'm not just any shaman, I'm a Sha256man


View Profile
February 14, 2013, 01:04:45 AM
 #22


Any decimal number can be exactly represented this way.


Unless you are using javascript which treats decimals as floats  Huh

I was waiting for someone else to point that out as well that there was no language being referenced.
Seal (OP)
Donator
Hero Member
*
Offline Offline

Activity: 848
Merit: 1078


View Profile WWW
February 19, 2013, 04:06:00 PM
 #23


Any decimal number can be exactly represented this way.


Unless you are using javascript which treats decimals as floats  Huh

I was waiting for someone else to point that out as well that there was no language being referenced.

Yes, the only way I thought of getting around this is to store multiple values in different formats for different languages. ie. store both integer and decimal or float.

It would be a pain otherwise to have a website which depends on javascript to manage the correct decimal display output. Every single value return or shown would require the use of javascript.

DefiDive - Filter the noise
A clean crypto asset management terminal
davout
Legendary
*
Offline Offline

Activity: 1372
Merit: 1007


1davout


View Profile WWW
February 19, 2013, 05:01:48 PM
 #24

Unless you are using javascript which treats decimals as floats  Huh
That just means javascript doesn't support decimals, only floats.
Using floats for display is fine in most cases, using floats for accounting is terribly wrong.

Zeilap
Full Member
***
Offline Offline

Activity: 154
Merit: 100


View Profile
February 19, 2013, 05:07:11 PM
 #25


Any decimal number can be exactly represented this way.


Unless you are using javascript which treats decimals as floats  Huh

I was waiting for someone else to point that out as well that there was no language being referenced.
No, this is not specific to javascript - no languages (that I can think of) provide built-in type support for arbitrary precision arithmetic, and for good reason - it is expensive in terms of computation.

What people are trying to say is that you should be using a library which provides a Decimal class which does provide this functionality (or a subset), and not use floating point values at all for computation.
davout
Legendary
*
Offline Offline

Activity: 1372
Merit: 1007


1davout


View Profile WWW
February 19, 2013, 05:39:52 PM
 #26

no languages (that I can think of) provide built-in type support for arbitrary precision arithmetic

and for good reason - it is expensive in terms of computation.
If I follow you, languages shouldn't provide standard interfaces to do network I/O, because you know, that's computationally expensive and slow. I guess I won't follow you...

Zeilap
Full Member
***
Offline Offline

Activity: 154
Merit: 100


View Profile
February 19, 2013, 07:03:46 PM
 #27

no languages (that I can think of) provide built-in type support for arbitrary precision arithmetic

and for good reason - it is expensive in terms of computation.
If I follow you, languages shouldn't provide standard interfaces to do network I/O, because you know, that's computationally expensive and slow. I guess I won't follow you...
They're not built-in types, they're types provided by a library. What the original poster seems (to me) to be confused about is that they can write code similar to
Code:
var x = 1.234
in some language and it will magically make x into a 'Decimal' object, and that only javascript will 'convert' this into a float/double. This is not true - they all will, as I say, because no language has a built-in type representing decimals, 1.234 is already a float before it gets assigned to anything. If you want decimals, then you have to specifically create one like
Code:
var x = new Decimal("1.234")
or however your library requires.
davout
Legendary
*
Offline Offline

Activity: 1372
Merit: 1007


1davout


View Profile WWW
February 19, 2013, 07:57:22 PM
 #28

They're not built-in types, they're types provided by a library.
I call built-in what's part of the standard library of a language. So to me it's built-in the languages I listed but not C++ or PHP for example, because these require an external library.

But yes, I don't know of any language that will natively interpret a numeric string as a decimal type without explicitly telling it to.

deepceleron
Legendary
*
Offline Offline

Activity: 1512
Merit: 1032



View Profile WWW
February 20, 2013, 01:28:44 AM
 #29

Your site may need more integer math accuracy than 1 satoshi = 1

example: 1 satoshi - 1.3% fee = .987 satoshis

>>> print int(.987)
0
>>> print int(round(.987))
1

However, even using arbitrary precision, these libraries don't necessary give you the correct answer anywhere near the magnitude of the float precision defined.

>>> getcontext().prec = 6
>>> Decimal (.00000001) * Decimal (.987)
Decimal('9.87000E-9')

>>>getcontext().prec = 2000
>>> Decimal (.00000001) * Decimal (.987)[/b]
Decimal('9.87000000000000009104248083235174268563047313600349515597682205532708579013502123676582211686536 538763903081417083740234375E-9')

It's close enough though - you'd get an extra 9 satoshi if your transaction was 10 billion BTC.
TheButterZone
Legendary
*
Offline Offline

Activity: 3052
Merit: 1031


RIP Mommy


View Profile WWW
February 20, 2013, 01:30:50 AM
 #30

Just going to leave this here, since I'm not a dev: https://github.com/zootreeves/blockchain.info/issues/5

Saying that you don't trust someone because of their behavior is completely valid.
Xenland
Legendary
*
Offline Offline

Activity: 980
Merit: 1003


I'm not just any shaman, I'm a Sha256man


View Profile
February 22, 2013, 02:04:51 AM
 #31

Your site may need more integer math accuracy than 1 satoshi = 1

example: 1 satoshi - 1.3% fee = .987 satoshis

>>> print int(.987)
0
>>> print int(round(.987))
1

However, even using arbitrary precision, these libraries don't necessary give you the correct answer anywhere near the magnitude of the float precision defined.

>>> getcontext().prec = 6
>>> Decimal (.00000001) * Decimal (.987)
Decimal('9.87000E-9')

>>>getcontext().prec = 2000
>>> Decimal (.00000001) * Decimal (.987)[/b]
Decimal('9.87000000000000009104248083235174268563047313600349515597682205532708579013502123676582211686536 538763903081417083740234375E-9')

It's close enough though - you'd get an extra 9 satoshi if your transaction was 10 billion BTC.

Assuming the total BTC in circulation limit was increased this could ever actually happen yes.
Pages: « 1 [2]  All
  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!