Telegram Group & Telegram Channel
https://github.com/shopspring/decimal?tab=readme-ov-file#faq

搜索 decimal 的库时看到这段 README 很有意思,介绍了为什么在金融应用中不能用有理数(Rational)来替代 decimal。

都知道浮点数只能表示近似值,一般不用于金融领域。而 Golang 中没有内建的 decimal 类型,有些人就提议说可以使用有理数 big.Rat 来代替。但是此文中举了一个通俗易懂的例子来反对这一观点,假设有三个等于 1/3 的有理数 a、b、c,使用 .FloatString(3) 将其转化为 float64 后的值为 0.333,可以看到它们相加后的总额中的 0.001 就消失了。

说到底就是有理数本身虽然没有精度损失,但是将其转换为 float 时会导致精度损失,毕竟你不可能直接给用户显示一个有理数。所以在金融应用中,最好不要使用有理数,而是直接使用整数。而 decimal 存储的实际上就是整数和小数点位置的偏移量。



tg-me.com/laiskynotes/378
Create:
Last Update:

https://github.com/shopspring/decimal?tab=readme-ov-file#faq

搜索 decimal 的库时看到这段 README 很有意思,介绍了为什么在金融应用中不能用有理数(Rational)来替代 decimal。

都知道浮点数只能表示近似值,一般不用于金融领域。而 Golang 中没有内建的 decimal 类型,有些人就提议说可以使用有理数 big.Rat 来代替。但是此文中举了一个通俗易懂的例子来反对这一观点,假设有三个等于 1/3 的有理数 a、b、c,使用 .FloatString(3) 将其转化为 float64 后的值为 0.333,可以看到它们相加后的总额中的 0.001 就消失了。

说到底就是有理数本身虽然没有精度损失,但是将其转换为 float 时会导致精度损失,毕竟你不可能直接给用户显示一个有理数。所以在金融应用中,最好不要使用有理数,而是直接使用整数。而 decimal 存储的实际上就是整数和小数点位置的偏移量。

BY Laisky's Notes


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/laiskynotes/378

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

Newly uncovered hack campaign in Telegram

The campaign, which security firm Check Point has named Rampant Kitten, comprises two main components, one for Windows and the other for Android. Rampant Kitten’s objective is to steal Telegram messages, passwords, and two-factor authentication codes sent by SMS and then also take screenshots and record sounds within earshot of an infected phone, the researchers said in a post published on Friday.

Telegram Auto-Delete Messages in Any Chat

Some messages aren’t supposed to last forever. There are some Telegram groups and conversations where it’s best if messages are automatically deleted in a day or a week. Here’s how to auto-delete messages in any Telegram chat. You can enable the auto-delete feature on a per-chat basis. It works for both one-on-one conversations and group chats. Previously, you needed to use the Secret Chat feature to automatically delete messages after a set time. At the time of writing, you can choose to automatically delete messages after a day or a week. Telegram starts the timer once they are sent, not after they are read. This won’t affect the messages that were sent before enabling the feature.

telegram from us


Telegram Laisky's Notes
FROM USA