mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-20 09:49:15 +03:00
LibCrypto: Add naive implementation of {Un,}SignedBigInteger::to_double
This commit is contained in:
parent
62bc238ac3
commit
bcdad57670
Notes:
sideshowbarker
2024-07-18 04:34:19 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/bcdad576700 Pull-request: https://github.com/SerenityOS/serenity/pull/9859 Reviewed-by: https://github.com/linusg ✅
@ -63,6 +63,14 @@ u64 SignedBigInteger::to_u64() const
|
||||
return ~(unsigned_value - 1); // equivalent to `-unsigned_value`, but doesnt trigger UBSAN
|
||||
}
|
||||
|
||||
double SignedBigInteger::to_double() const
|
||||
{
|
||||
double unsigned_value = m_unsigned_data.to_double();
|
||||
if (!m_sign)
|
||||
return unsigned_value;
|
||||
return -unsigned_value;
|
||||
}
|
||||
|
||||
FLATTEN SignedBigInteger SignedBigInteger::plus(const SignedBigInteger& other) const
|
||||
{
|
||||
// If both are of the same sign, just add the unsigned data and return.
|
||||
|
@ -66,6 +66,7 @@ public:
|
||||
String to_base(u16 N) const;
|
||||
|
||||
u64 to_u64() const;
|
||||
double to_double() const;
|
||||
|
||||
const UnsignedBigInteger& unsigned_value() const { return m_unsigned_data; }
|
||||
const Vector<u32, STARTING_WORD_SIZE> words() const { return m_unsigned_data.words(); }
|
||||
|
@ -112,6 +112,12 @@ u64 UnsignedBigInteger::to_u64() const
|
||||
return value;
|
||||
}
|
||||
|
||||
double UnsignedBigInteger::to_double() const
|
||||
{
|
||||
// FIXME: I am naive
|
||||
return static_cast<double>(to_u64());
|
||||
}
|
||||
|
||||
void UnsignedBigInteger::set_to_0()
|
||||
{
|
||||
m_words.clear_with_capacity();
|
||||
|
@ -57,6 +57,7 @@ public:
|
||||
String to_base(u16 N) const;
|
||||
|
||||
u64 to_u64() const;
|
||||
double to_double() const;
|
||||
|
||||
const Vector<Word, STARTING_WORD_SIZE>& words() const { return m_words; }
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user