Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 9c5083534ca6
Choose a base ref
...
head repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 724699195e2e
Choose a head ref
  • 11 commits
  • 2 files changed
  • 1 contributor

Commits on Dec 29, 2020

  1. Copy the full SHA
    b49784e View commit details
  2. Copy the full SHA
    aefac16 View commit details
  3. Copy the full SHA
    ff96a20 View commit details
  4. Copy the full SHA
    0824a98 View commit details
  5. test

    eggrobin committed Dec 29, 2020
    Copy the full SHA
    e914b5b View commit details

Commits on Dec 30, 2020

  1. Copy the full SHA
    61bc963 View commit details
  2. Copy the full SHA
    3985f46 View commit details
  3. β

    eggrobin committed Dec 30, 2020
    Copy the full SHA
    5f21e7a View commit details
  4. Copy the full SHA
    b687296 View commit details
  5. iwyu

    eggrobin committed Dec 30, 2020
    Copy the full SHA
    924687e View commit details

Commits on Dec 31, 2020

  1. Merge pull request #2830 from eggrobin/exact-mathematica

    Exact mathematica
    eggrobin authored Dec 31, 2020
    Copy the full SHA
    7246991 View commit details
Showing with 48 additions and 15 deletions.
  1. +33 −9 mathematica/mathematica_body.hpp
  2. +15 −6 mathematica/mathematica_test.cpp
42 changes: 33 additions & 9 deletions mathematica/mathematica_body.hpp
Original file line number Diff line number Diff line change
@@ -4,11 +4,14 @@
#include "mathematica/mathematica.hpp"

#include <cmath>
#include <limits>
#include <map>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>

#include "base/mod.hpp"
#include "base/not_constructible.hpp"
#include "base/traits.hpp"
#include "quantities/si.hpp"
@@ -19,6 +22,7 @@ namespace internal_mathematica {

using astronomy::J2000;
using base::is_instance_of_v;
using base::mod;
using base::not_constructible;
using base::not_null;
using quantities::DebugString;
@@ -264,19 +268,39 @@ std::string ToMathematica(T const integer, OptionalExpressIn /*express_in*/) {
template<typename T, typename, typename OptionalExpressIn, typename>
std::string ToMathematica(T const real,
OptionalExpressIn /*express_in*/) {
std::string absolute_value;
if (std::isinf(real)) {
if (real > 0.0) {
return "Infinity";
} else {
return Apply("Minus", {"Infinity"});
}
absolute_value = "Infinity";
} else if (std::isnan(real)) {
return "Indeterminate";
absolute_value = "Indeterminate";
} else if (real == 0) {
absolute_value = "0";
} else {
std::string s = DebugString(real);
s.replace(s.find("e"), 1, "*^");
return Apply("SetPrecision", {s, "$MachinePrecision"});
constexpr int τ = std::numeric_limits<T>::digits;
int const exponent = std::ilogb(real);
// This offset makes n an integer in [β^(τ-1), β^τ[, i.e., a τ-digit
// integer.
int exponent_offset = τ - 1;
if (std::numeric_limits<T>::radix == 2) {
// For binary floating point, push the leading 1 to the least significant
// bit of a hex digit.
exponent_offset += mod(1 - τ, 4);
}
std::int64_t const n =
std::scalbln(std::abs(real), exponent_offset - exponent);
absolute_value = Apply("Times",
{std::numeric_limits<T>::radix == 10
? ToMathematica(n)
: (std::stringstream()
<< "16^^" << std::uppercase << std::hex << n)
.str(),
Apply("Power",
{"2",
Apply("Subtract",
{ToMathematica(std::ilogb(real)),
ToMathematica(exponent_offset)})})});
}
return std::signbit(real) ? Apply("Minus", {absolute_value}) : absolute_value;
}

template<typename OptionalExpressIn>
21 changes: 15 additions & 6 deletions mathematica/mathematica_test.cpp
Original file line number Diff line number Diff line change
@@ -52,8 +52,10 @@ using numerics::UnboundedUpperTriangularMatrix;
using numerics::UnboundedVector;
using physics::DegreesOfFreedom;
using physics::DiscreteTrajectory;
using quantities::Infinity;
using quantities::Length;
using quantities::Speed;
using quantities::Sqrt;
using quantities::Time;
using quantities::si::Degree;
using quantities::si::Metre;
@@ -100,12 +102,19 @@ TEST_F(MathematicaTest, ToMathematica) {
EXPECT_EQ("-2", ToMathematica(-2));
}
{
EXPECT_EQ("SetPrecision[+3.00000000000000000*^+00,$MachinePrecision]",
ToMathematica(3.0));
EXPECT_EQ("SetPrecision[-2.00000000000000000*^+09,$MachinePrecision]",
ToMathematica(-2.0e9));
EXPECT_EQ("SetPrecision[-0.00000000000000000*^+00,$MachinePrecision]",
ToMathematica(-0.0));
EXPECT_EQ("Times[16^^13456789ABCDEF,Power[2,Subtract[-163,52]]]",
ToMathematica(0x1.3'4567'89AB'CDEFp-163));
EXPECT_EQ("Minus[Times[16^^10000000000000,Power[2,Subtract[-1074,52]]]]",
ToMathematica(-0x1p-1074));
EXPECT_EQ("Minus[Times[16^^1FFFFFFFFFFFFF,Power[2,Subtract[1023,52]]]]",
ToMathematica(-0x1.F'FFFF'FFFF'FFFFp1023));
EXPECT_EQ("Times[16^^19ABCDE,Power[2,Subtract[-12,24]]]",
ToMathematica(0x1.9ABCDEp-12f));
EXPECT_EQ("0", ToMathematica(0.0));
EXPECT_EQ("Minus[0]", ToMathematica(-0.0)); // Not that this does anything.
EXPECT_EQ("Infinity", ToMathematica(Infinity<double>));
EXPECT_EQ("Minus[Infinity]", ToMathematica(-Infinity<double>));
EXPECT_EQ("Minus[Indeterminate]", ToMathematica(Sqrt(-1)));
}
{
EXPECT_EQ(ToMathematica(std::tuple{2.0, 3.0}),