The picture shows two trips from 0.1 to binary and back. Stata's eight-digit trip returns 0.09765625; Mata's 56-digit trip displays 0.100000000000000006. Match Mata's limit to eight digits and the answers match. No data or add-ons are needed. Paste the two blocks into one do-file.
Stata, eight binary fractional digits:
Code:
version 16.1
clear all
scalar rem = 0.1
local bits ""
forvalues j = 1/8 {
scalar rem = 2*rem
local digit = floor(rem)
scalar rem = rem - `digit'
local bits "`bits'`digit'"
}
scalar answer = 0
forvalues j = 1/8 {
local digit = real(substr("`bits'", `j', 1))
scalar answer = answer + `digit'/2^`j'
}
display "Stata, 8 digits: 0.`bits' -> " %21.18f scalar(answer)
Code:
mata:
x = 0.1
e56 = .
b56 = inbase(2, x, 56, e56)
printf("Mata, 56 digits: %21.18f\n", frombase(2, b56))
e8 = .
b8 = inbase(2, x, 8, e8)
printf("Mata, 8 digits: %s -> %21.18f\n", b8, frombase(2, b8))
end
Mata reference: https://www.stata.com/manuals/m-5inbase.pdf

Comment