Serenity Operating System
at master 45 lines 1.4 kB view raw
1/* 2 * Copyright (c) 2022, Dan Klishch <danilklishch@gmail.com> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#pragma once 8 9#ifdef KERNEL 10# error This file should not be included in the KERNEL, as doubles should not appear in the \ 11 kernel code, although the conversion currently does not use any floating point \ 12 computations. 13#endif 14 15#include <AK/Concepts.h> 16#include <AK/Types.h> 17 18namespace AK { 19 20struct FloatingPointExponentialForm { 21 bool sign; 22 u64 fraction; 23 i32 exponent; 24}; 25 26/// This function finds the representation of `value' in the form of 27/// `(-1) ^ sign * fraction * 10 ^ exponent', such that (applying in the order of enumeration): 28/// 29/// 1. sign is either 0 or 1, fraction is a non-negative number, exponent is an integer. 30/// 2. For +0, it is {.sign = 0, .fraction = 0, .exponent = 0}, 31/// for -0, is {.sign = 1, .fraction = 0, .exponent = 0}, 32/// for +inf, -inf, and NaN is undefined. 33/// 3. `(-1) ^ sign * fraction * 10 ^ exponent', computed with an infinite precision, rounds to 34/// `value' in the half to even mode. 35/// 4. len(str(fraction)) is minimal. 36/// 5. `abs((-1) ^ sign * fraction * 10 ^ exponent - value)' is minimal. 37/// 6. fraction is even. 38template<FloatingPoint T> 39FloatingPointExponentialForm convert_floating_point_to_decimal_exponential_form(T value); 40 41} 42 43#if USING_AK_GLOBALLY 44using AK::convert_floating_point_to_decimal_exponential_form; 45#endif