Skip to content

Instantly share code, notes, and snippets.

@djnavarro
Created October 14, 2020 21:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djnavarro/d565ca2304974db3ecc8741b5e318e4e to your computer and use it in GitHub Desktop.
Save djnavarro/d565ca2304974db3ecc8741b5e318e4e to your computer and use it in GitHub Desktop.
computes floating point errors
library(ggplot2)
library(dplyr)
library(paletteer)
# specify the range
lo <- 0
hi <- 4
by <- .01
# create data structure
val <- seq(lo, hi, by)
dat <- expand.grid(
x = val,
y = val,
z = seq(lo, 2*hi, by)
)
dat <- dat %>%
mutate(s = x + y - z) %>% # compute all sums
filter(abs(s) < by/10) %>% # retain ostensibly zero rows
arrange(x, y) # be pretty
# create plot object
pic <- ggplot(dat, aes(x, y, fill = s)) +
geom_raster(show.legend = FALSE) +
theme_void() +
coord_fixed() +
scale_fill_paletteer_c("scico::broc") +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
NULL
# draw plot
ggsave(
filename = here::here("image", "floating_point_01.png"),
plot = pic,
height = 5000/300,
width = 5000/300,
dpi = 300
)
@djnavarro
Copy link
Author

floating_point_01

@remokon
Copy link

remokon commented Oct 15, 2020

This is so beautiful 😍 thanks for sharing it Danielle!

@djnavarro
Copy link
Author

❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment