Test of integers? Or, round the number if the first two decimal numbers are sufficiently close to 0 or 1?
![Multi tool use Multi tool use](http://sgv.ssvwv.com/sg/ssvwvcomimagb.png)
Multi tool use
The following is a MWE, which explains my intention.
documentclass{article}
usepackage{tikz}
usetikzlibrary{math}
begin{document}
tikzmath{
integer = 4/2; decimal = 5/3;
integerB=1/3*3;
}
$integer$ is an integer, and it should be printed as 2.
And $decimal$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
end{document}
I wonder if it is possible to test a number to determine it is an integer. Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1.
tables tikzmath decimal-number
add a comment |
The following is a MWE, which explains my intention.
documentclass{article}
usepackage{tikz}
usetikzlibrary{math}
begin{document}
tikzmath{
integer = 4/2; decimal = 5/3;
integerB=1/3*3;
}
$integer$ is an integer, and it should be printed as 2.
And $decimal$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
end{document}
I wonder if it is possible to test a number to determine it is an integer. Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1.
tables tikzmath decimal-number
add a comment |
The following is a MWE, which explains my intention.
documentclass{article}
usepackage{tikz}
usetikzlibrary{math}
begin{document}
tikzmath{
integer = 4/2; decimal = 5/3;
integerB=1/3*3;
}
$integer$ is an integer, and it should be printed as 2.
And $decimal$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
end{document}
I wonder if it is possible to test a number to determine it is an integer. Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1.
tables tikzmath decimal-number
The following is a MWE, which explains my intention.
documentclass{article}
usepackage{tikz}
usetikzlibrary{math}
begin{document}
tikzmath{
integer = 4/2; decimal = 5/3;
integerB=1/3*3;
}
$integer$ is an integer, and it should be printed as 2.
And $decimal$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
end{document}
I wonder if it is possible to test a number to determine it is an integer. Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1.
tables tikzmath decimal-number
tables tikzmath decimal-number
edited Dec 31 '18 at 16:11
![](https://i.stack.imgur.com/wJWMb.png?s=32&g=1)
![](https://i.stack.imgur.com/wJWMb.png?s=32&g=1)
egreg
719k8719053205
719k8719053205
asked Dec 31 '18 at 15:58
GlennGlenn
432
432
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
documentclass{article}
usepackage{xintexpr}% recommended package
newcommandtest[1]{xintifboolexpr{ifint(#1, 1, 0)}
{xinttheexpr reduce(#1)relaxspace (originally texttt{detokenize{#1}})
is an integer.}
{xinttheiexpr[2] #1relaxspace (originally texttt{detokenize{#1}}),
we rounded to two decimal places) is not an integer.}par
}
begin{document}
test{4/2}
test{5/3}
test{(1/3)*3}
test{(1/7 - 1/8 - 1/57)*3192}
test{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}
end{document}
strangely xintexpr hasifint(expression, YES, NO)
function but notisint(expression)
which would evaluate to 1 or 0 directly.
– user4686
Jan 1 at 13:21
about "Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1." this can be done by xintexpr of course as it computes exactly with arbitrarily big fractions. But I don't know exactly what is asked here.
– user4686
Jan 1 at 13:23
for comparisonfpeval{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}
evaluates to0.9999211586333241
, exactly likexintthefloatexpr...relax
. But the latter afterxintDigits:=48;
will evaluate to1.00000000000000000000000000000000000522205263811
.
– user4686
Jan 1 at 13:46
FWIW, using the default precision settings, Lua evaluates(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806
as1.000285363444
. Of course,round{comp{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}}{1}
(whereround
andcomp
are defined in my answer) produces1
. Whew!
– Mico
Jan 1 at 14:08
add a comment |
One cannot say from its floating point representation whether the output of an arithmetic operation involving division or non rational operations is actually an integer.
You can consider the l3fp
module of expl3
, available through the package xfp
.
documentclass{article}
usepackage{xfp}
begin{document}
$fpeval{5/3}$ is a decimal number, I would like to round it to
$fpeval{round(5/3,1)}$ or to $fpeval{round(5/3,2)}$
Another difficulty is that $fpeval{(1/3)*3}$ is an integer in fact,
and should be printed as $fpeval{round((1/3)*3,1)}$ or
$fpeval{round((1/3)*3,2)}$.
end{document}
add a comment |
If the accuracy of your numbers is important you might consider farming that out to a computer algebra system (CAS). The sagetex
package relies on the CAS Sage; the documentation can be found on CTAN right here. Documentation on Sage is found here .Sage is not part of the LaTeX distribution (it's big) so it needs to be installed on your computer or, even easier, accessed through a free Cocalc account.
documentclass{article}
usepackage{sagetex}
usepackage{tikz}
usetikzlibrary{math}
begin{document}
begin{sagesilent}
a = 4/2
b = 5/3
c = 1/3*3
end{sagesilent}
$sage{a}$ is an integer, and it should be printed as $sage{a}$.
And $sage{b}$ is not an integer. As a decimal it is approximately
$sage{b.n(digits=6)}$. I would like to round it to $sage{b.n(digits=1)}$.
Another difficulty is that $sage{c}$ is an integer in fact,
and should be printed as $sage{c}$.
end{document}
The output, running in Cocalc, gives:
Notice that Sage interprets your numbers correctly: 4/2 is recognized as 2 and 1/3*3 is recognized as 1. It does need to know the format you want of non integers; but it recognizes that 5/3 is a fraction that can't be reduced and leaves it as a fraction. To force it into a decimal and to specify the number of digits we append .n(digits=6); the documentation is here.
add a comment |
Assuming that this is a question on how to do this with tikzmath, I'd do
documentclass{article}
usepackage{tikz}
usetikzlibrary{math}
begin{document}
tikzmath{
function myint(x) {
if abs(x-round(x)) < 0.1 then { print{x is an integernewline};
return int(round(x));
} else { print{x is not an integernewline};
return x;
}; };
integer = myint(4/2);
decimal = myint(5/3);
integerB= myint(1/3*3);
}
$integer$ is an integer, and it should be printed as 2.
And $pgfmathprintnumber[precision=1]{decimal}$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
end{document}
Note that I used pgfmathprintnumber
to round to one digit after the dot. Of course, you can remove the print
s.
add a comment |
Here's a LuaLaTeX-based solution. integer
(4/2) and integerB
((1/3)*3) evaluate to integers automatically according to Lua rules. The LaTeX macro round
, which takes two arguments, lets users round numbers to a specified set of digits after the decimal marker.
% !TEX TS-program = lualatex
documentclass{article}
% Set up 2 auxilliary Lua functions to round numbers
directlua{
function math.round_int ( x )
return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
end
function math.round ( x , n )
return ( math.round_int ( x*10^n ) / 10^n )
end
}
newcommandcomp[1]{directlua{tex.sprint(#1)}}
newcommandround[2]{directlua{tex.sprint(math.round(#1,#2))}}
newcommand{integer}{comp{4/2}}
newcommand{decimal}{comp{5/3}}
newcommand{integerB}{comp{(1/3)*3}}
begin{document}
integer is an integer.
integerB is also an integer.
decimal is a decimal number. I would like to round it to round{decimal}{1}.
end{document}
1
does(1/7-1/8-1/57)*3192
evaluate to an integer in Lua? (just curious...)
– user4686
Jan 1 at 13:37
1
in case answer is yes (perhaps from some distributivity done automatically), I have(1/7 - 1/8 - 1/57)*(3000+192)
up my sleeve :)
– user4686
Jan 1 at 13:43
@jfbu - Both expressions evaluate to0.99999999999998
(13 nines followed by an 8), using the standard number of significant digits used intex.sprint
. If I reduced that number by 1 or 2 digits, one is back to1
(pun intended).
– Mico
Jan 1 at 13:46
2
I thinkxintthefloatexpr
works withxintDigits:=2;
but I am not sure with only 1 digit :) you are really quite a challenge!
– user4686
Jan 1 at 13:53
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f468046%2ftest-of-integers-or-round-the-number-if-the-first-two-decimal-numbers-are-suff%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
documentclass{article}
usepackage{xintexpr}% recommended package
newcommandtest[1]{xintifboolexpr{ifint(#1, 1, 0)}
{xinttheexpr reduce(#1)relaxspace (originally texttt{detokenize{#1}})
is an integer.}
{xinttheiexpr[2] #1relaxspace (originally texttt{detokenize{#1}}),
we rounded to two decimal places) is not an integer.}par
}
begin{document}
test{4/2}
test{5/3}
test{(1/3)*3}
test{(1/7 - 1/8 - 1/57)*3192}
test{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}
end{document}
strangely xintexpr hasifint(expression, YES, NO)
function but notisint(expression)
which would evaluate to 1 or 0 directly.
– user4686
Jan 1 at 13:21
about "Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1." this can be done by xintexpr of course as it computes exactly with arbitrarily big fractions. But I don't know exactly what is asked here.
– user4686
Jan 1 at 13:23
for comparisonfpeval{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}
evaluates to0.9999211586333241
, exactly likexintthefloatexpr...relax
. But the latter afterxintDigits:=48;
will evaluate to1.00000000000000000000000000000000000522205263811
.
– user4686
Jan 1 at 13:46
FWIW, using the default precision settings, Lua evaluates(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806
as1.000285363444
. Of course,round{comp{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}}{1}
(whereround
andcomp
are defined in my answer) produces1
. Whew!
– Mico
Jan 1 at 14:08
add a comment |
documentclass{article}
usepackage{xintexpr}% recommended package
newcommandtest[1]{xintifboolexpr{ifint(#1, 1, 0)}
{xinttheexpr reduce(#1)relaxspace (originally texttt{detokenize{#1}})
is an integer.}
{xinttheiexpr[2] #1relaxspace (originally texttt{detokenize{#1}}),
we rounded to two decimal places) is not an integer.}par
}
begin{document}
test{4/2}
test{5/3}
test{(1/3)*3}
test{(1/7 - 1/8 - 1/57)*3192}
test{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}
end{document}
strangely xintexpr hasifint(expression, YES, NO)
function but notisint(expression)
which would evaluate to 1 or 0 directly.
– user4686
Jan 1 at 13:21
about "Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1." this can be done by xintexpr of course as it computes exactly with arbitrarily big fractions. But I don't know exactly what is asked here.
– user4686
Jan 1 at 13:23
for comparisonfpeval{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}
evaluates to0.9999211586333241
, exactly likexintthefloatexpr...relax
. But the latter afterxintDigits:=48;
will evaluate to1.00000000000000000000000000000000000522205263811
.
– user4686
Jan 1 at 13:46
FWIW, using the default precision settings, Lua evaluates(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806
as1.000285363444
. Of course,round{comp{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}}{1}
(whereround
andcomp
are defined in my answer) produces1
. Whew!
– Mico
Jan 1 at 14:08
add a comment |
documentclass{article}
usepackage{xintexpr}% recommended package
newcommandtest[1]{xintifboolexpr{ifint(#1, 1, 0)}
{xinttheexpr reduce(#1)relaxspace (originally texttt{detokenize{#1}})
is an integer.}
{xinttheiexpr[2] #1relaxspace (originally texttt{detokenize{#1}}),
we rounded to two decimal places) is not an integer.}par
}
begin{document}
test{4/2}
test{5/3}
test{(1/3)*3}
test{(1/7 - 1/8 - 1/57)*3192}
test{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}
end{document}
documentclass{article}
usepackage{xintexpr}% recommended package
newcommandtest[1]{xintifboolexpr{ifint(#1, 1, 0)}
{xinttheexpr reduce(#1)relaxspace (originally texttt{detokenize{#1}})
is an integer.}
{xinttheiexpr[2] #1relaxspace (originally texttt{detokenize{#1}}),
we rounded to two decimal places) is not an integer.}par
}
begin{document}
test{4/2}
test{5/3}
test{(1/3)*3}
test{(1/7 - 1/8 - 1/57)*3192}
test{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}
end{document}
edited Jan 1 at 13:40
answered Jan 1 at 13:20
user4686
strangely xintexpr hasifint(expression, YES, NO)
function but notisint(expression)
which would evaluate to 1 or 0 directly.
– user4686
Jan 1 at 13:21
about "Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1." this can be done by xintexpr of course as it computes exactly with arbitrarily big fractions. But I don't know exactly what is asked here.
– user4686
Jan 1 at 13:23
for comparisonfpeval{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}
evaluates to0.9999211586333241
, exactly likexintthefloatexpr...relax
. But the latter afterxintDigits:=48;
will evaluate to1.00000000000000000000000000000000000522205263811
.
– user4686
Jan 1 at 13:46
FWIW, using the default precision settings, Lua evaluates(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806
as1.000285363444
. Of course,round{comp{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}}{1}
(whereround
andcomp
are defined in my answer) produces1
. Whew!
– Mico
Jan 1 at 14:08
add a comment |
strangely xintexpr hasifint(expression, YES, NO)
function but notisint(expression)
which would evaluate to 1 or 0 directly.
– user4686
Jan 1 at 13:21
about "Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1." this can be done by xintexpr of course as it computes exactly with arbitrarily big fractions. But I don't know exactly what is asked here.
– user4686
Jan 1 at 13:23
for comparisonfpeval{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}
evaluates to0.9999211586333241
, exactly likexintthefloatexpr...relax
. But the latter afterxintDigits:=48;
will evaluate to1.00000000000000000000000000000000000522205263811
.
– user4686
Jan 1 at 13:46
FWIW, using the default precision settings, Lua evaluates(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806
as1.000285363444
. Of course,round{comp{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}}{1}
(whereround
andcomp
are defined in my answer) produces1
. Whew!
– Mico
Jan 1 at 14:08
strangely xintexpr has
ifint(expression, YES, NO)
function but not isint(expression)
which would evaluate to 1 or 0 directly.– user4686
Jan 1 at 13:21
strangely xintexpr has
ifint(expression, YES, NO)
function but not isint(expression)
which would evaluate to 1 or 0 directly.– user4686
Jan 1 at 13:21
about "Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1." this can be done by xintexpr of course as it computes exactly with arbitrarily big fractions. But I don't know exactly what is asked here.
– user4686
Jan 1 at 13:23
about "Alternatively, it would also be great if it is possible to determine whether the first two decimal numbers are sufficiently close to 0 or 1." this can be done by xintexpr of course as it computes exactly with arbitrarily big fractions. But I don't know exactly what is asked here.
– user4686
Jan 1 at 13:23
for comparison
fpeval{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}
evaluates to 0.9999211586333241
, exactly like xintthefloatexpr...relax
. But the latter after xintDigits:=48;
will evaluate to 1.00000000000000000000000000000000000522205263811
.– user4686
Jan 1 at 13:46
for comparison
fpeval{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}
evaluates to 0.9999211586333241
, exactly like xintthefloatexpr...relax
. But the latter after xintDigits:=48;
will evaluate to 1.00000000000000000000000000000000000522205263811
.– user4686
Jan 1 at 13:46
FWIW, using the default precision settings, Lua evaluates
(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806
as 1.000285363444
. Of course, round{comp{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}}{1}
(where round
and comp
are defined in my answer) produces 1
. Whew!– Mico
Jan 1 at 14:08
FWIW, using the default precision settings, Lua evaluates
(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806
as 1.000285363444
. Of course, round{comp{(1/2 - 1/3 - 1/7 - 1/43 - 1/1807 - 1/3263443)*10650056950806}}{1}
(where round
and comp
are defined in my answer) produces 1
. Whew!– Mico
Jan 1 at 14:08
add a comment |
One cannot say from its floating point representation whether the output of an arithmetic operation involving division or non rational operations is actually an integer.
You can consider the l3fp
module of expl3
, available through the package xfp
.
documentclass{article}
usepackage{xfp}
begin{document}
$fpeval{5/3}$ is a decimal number, I would like to round it to
$fpeval{round(5/3,1)}$ or to $fpeval{round(5/3,2)}$
Another difficulty is that $fpeval{(1/3)*3}$ is an integer in fact,
and should be printed as $fpeval{round((1/3)*3,1)}$ or
$fpeval{round((1/3)*3,2)}$.
end{document}
add a comment |
One cannot say from its floating point representation whether the output of an arithmetic operation involving division or non rational operations is actually an integer.
You can consider the l3fp
module of expl3
, available through the package xfp
.
documentclass{article}
usepackage{xfp}
begin{document}
$fpeval{5/3}$ is a decimal number, I would like to round it to
$fpeval{round(5/3,1)}$ or to $fpeval{round(5/3,2)}$
Another difficulty is that $fpeval{(1/3)*3}$ is an integer in fact,
and should be printed as $fpeval{round((1/3)*3,1)}$ or
$fpeval{round((1/3)*3,2)}$.
end{document}
add a comment |
One cannot say from its floating point representation whether the output of an arithmetic operation involving division or non rational operations is actually an integer.
You can consider the l3fp
module of expl3
, available through the package xfp
.
documentclass{article}
usepackage{xfp}
begin{document}
$fpeval{5/3}$ is a decimal number, I would like to round it to
$fpeval{round(5/3,1)}$ or to $fpeval{round(5/3,2)}$
Another difficulty is that $fpeval{(1/3)*3}$ is an integer in fact,
and should be printed as $fpeval{round((1/3)*3,1)}$ or
$fpeval{round((1/3)*3,2)}$.
end{document}
One cannot say from its floating point representation whether the output of an arithmetic operation involving division or non rational operations is actually an integer.
You can consider the l3fp
module of expl3
, available through the package xfp
.
documentclass{article}
usepackage{xfp}
begin{document}
$fpeval{5/3}$ is a decimal number, I would like to round it to
$fpeval{round(5/3,1)}$ or to $fpeval{round(5/3,2)}$
Another difficulty is that $fpeval{(1/3)*3}$ is an integer in fact,
and should be printed as $fpeval{round((1/3)*3,1)}$ or
$fpeval{round((1/3)*3,2)}$.
end{document}
answered Dec 31 '18 at 16:10
![](https://i.stack.imgur.com/wJWMb.png?s=32&g=1)
![](https://i.stack.imgur.com/wJWMb.png?s=32&g=1)
egregegreg
719k8719053205
719k8719053205
add a comment |
add a comment |
If the accuracy of your numbers is important you might consider farming that out to a computer algebra system (CAS). The sagetex
package relies on the CAS Sage; the documentation can be found on CTAN right here. Documentation on Sage is found here .Sage is not part of the LaTeX distribution (it's big) so it needs to be installed on your computer or, even easier, accessed through a free Cocalc account.
documentclass{article}
usepackage{sagetex}
usepackage{tikz}
usetikzlibrary{math}
begin{document}
begin{sagesilent}
a = 4/2
b = 5/3
c = 1/3*3
end{sagesilent}
$sage{a}$ is an integer, and it should be printed as $sage{a}$.
And $sage{b}$ is not an integer. As a decimal it is approximately
$sage{b.n(digits=6)}$. I would like to round it to $sage{b.n(digits=1)}$.
Another difficulty is that $sage{c}$ is an integer in fact,
and should be printed as $sage{c}$.
end{document}
The output, running in Cocalc, gives:
Notice that Sage interprets your numbers correctly: 4/2 is recognized as 2 and 1/3*3 is recognized as 1. It does need to know the format you want of non integers; but it recognizes that 5/3 is a fraction that can't be reduced and leaves it as a fraction. To force it into a decimal and to specify the number of digits we append .n(digits=6); the documentation is here.
add a comment |
If the accuracy of your numbers is important you might consider farming that out to a computer algebra system (CAS). The sagetex
package relies on the CAS Sage; the documentation can be found on CTAN right here. Documentation on Sage is found here .Sage is not part of the LaTeX distribution (it's big) so it needs to be installed on your computer or, even easier, accessed through a free Cocalc account.
documentclass{article}
usepackage{sagetex}
usepackage{tikz}
usetikzlibrary{math}
begin{document}
begin{sagesilent}
a = 4/2
b = 5/3
c = 1/3*3
end{sagesilent}
$sage{a}$ is an integer, and it should be printed as $sage{a}$.
And $sage{b}$ is not an integer. As a decimal it is approximately
$sage{b.n(digits=6)}$. I would like to round it to $sage{b.n(digits=1)}$.
Another difficulty is that $sage{c}$ is an integer in fact,
and should be printed as $sage{c}$.
end{document}
The output, running in Cocalc, gives:
Notice that Sage interprets your numbers correctly: 4/2 is recognized as 2 and 1/3*3 is recognized as 1. It does need to know the format you want of non integers; but it recognizes that 5/3 is a fraction that can't be reduced and leaves it as a fraction. To force it into a decimal and to specify the number of digits we append .n(digits=6); the documentation is here.
add a comment |
If the accuracy of your numbers is important you might consider farming that out to a computer algebra system (CAS). The sagetex
package relies on the CAS Sage; the documentation can be found on CTAN right here. Documentation on Sage is found here .Sage is not part of the LaTeX distribution (it's big) so it needs to be installed on your computer or, even easier, accessed through a free Cocalc account.
documentclass{article}
usepackage{sagetex}
usepackage{tikz}
usetikzlibrary{math}
begin{document}
begin{sagesilent}
a = 4/2
b = 5/3
c = 1/3*3
end{sagesilent}
$sage{a}$ is an integer, and it should be printed as $sage{a}$.
And $sage{b}$ is not an integer. As a decimal it is approximately
$sage{b.n(digits=6)}$. I would like to round it to $sage{b.n(digits=1)}$.
Another difficulty is that $sage{c}$ is an integer in fact,
and should be printed as $sage{c}$.
end{document}
The output, running in Cocalc, gives:
Notice that Sage interprets your numbers correctly: 4/2 is recognized as 2 and 1/3*3 is recognized as 1. It does need to know the format you want of non integers; but it recognizes that 5/3 is a fraction that can't be reduced and leaves it as a fraction. To force it into a decimal and to specify the number of digits we append .n(digits=6); the documentation is here.
If the accuracy of your numbers is important you might consider farming that out to a computer algebra system (CAS). The sagetex
package relies on the CAS Sage; the documentation can be found on CTAN right here. Documentation on Sage is found here .Sage is not part of the LaTeX distribution (it's big) so it needs to be installed on your computer or, even easier, accessed through a free Cocalc account.
documentclass{article}
usepackage{sagetex}
usepackage{tikz}
usetikzlibrary{math}
begin{document}
begin{sagesilent}
a = 4/2
b = 5/3
c = 1/3*3
end{sagesilent}
$sage{a}$ is an integer, and it should be printed as $sage{a}$.
And $sage{b}$ is not an integer. As a decimal it is approximately
$sage{b.n(digits=6)}$. I would like to round it to $sage{b.n(digits=1)}$.
Another difficulty is that $sage{c}$ is an integer in fact,
and should be printed as $sage{c}$.
end{document}
The output, running in Cocalc, gives:
Notice that Sage interprets your numbers correctly: 4/2 is recognized as 2 and 1/3*3 is recognized as 1. It does need to know the format you want of non integers; but it recognizes that 5/3 is a fraction that can't be reduced and leaves it as a fraction. To force it into a decimal and to specify the number of digits we append .n(digits=6); the documentation is here.
answered Dec 31 '18 at 16:41
DJPDJP
7,17921630
7,17921630
add a comment |
add a comment |
Assuming that this is a question on how to do this with tikzmath, I'd do
documentclass{article}
usepackage{tikz}
usetikzlibrary{math}
begin{document}
tikzmath{
function myint(x) {
if abs(x-round(x)) < 0.1 then { print{x is an integernewline};
return int(round(x));
} else { print{x is not an integernewline};
return x;
}; };
integer = myint(4/2);
decimal = myint(5/3);
integerB= myint(1/3*3);
}
$integer$ is an integer, and it should be printed as 2.
And $pgfmathprintnumber[precision=1]{decimal}$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
end{document}
Note that I used pgfmathprintnumber
to round to one digit after the dot. Of course, you can remove the print
s.
add a comment |
Assuming that this is a question on how to do this with tikzmath, I'd do
documentclass{article}
usepackage{tikz}
usetikzlibrary{math}
begin{document}
tikzmath{
function myint(x) {
if abs(x-round(x)) < 0.1 then { print{x is an integernewline};
return int(round(x));
} else { print{x is not an integernewline};
return x;
}; };
integer = myint(4/2);
decimal = myint(5/3);
integerB= myint(1/3*3);
}
$integer$ is an integer, and it should be printed as 2.
And $pgfmathprintnumber[precision=1]{decimal}$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
end{document}
Note that I used pgfmathprintnumber
to round to one digit after the dot. Of course, you can remove the print
s.
add a comment |
Assuming that this is a question on how to do this with tikzmath, I'd do
documentclass{article}
usepackage{tikz}
usetikzlibrary{math}
begin{document}
tikzmath{
function myint(x) {
if abs(x-round(x)) < 0.1 then { print{x is an integernewline};
return int(round(x));
} else { print{x is not an integernewline};
return x;
}; };
integer = myint(4/2);
decimal = myint(5/3);
integerB= myint(1/3*3);
}
$integer$ is an integer, and it should be printed as 2.
And $pgfmathprintnumber[precision=1]{decimal}$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
end{document}
Note that I used pgfmathprintnumber
to round to one digit after the dot. Of course, you can remove the print
s.
Assuming that this is a question on how to do this with tikzmath, I'd do
documentclass{article}
usepackage{tikz}
usetikzlibrary{math}
begin{document}
tikzmath{
function myint(x) {
if abs(x-round(x)) < 0.1 then { print{x is an integernewline};
return int(round(x));
} else { print{x is not an integernewline};
return x;
}; };
integer = myint(4/2);
decimal = myint(5/3);
integerB= myint(1/3*3);
}
$integer$ is an integer, and it should be printed as 2.
And $pgfmathprintnumber[precision=1]{decimal}$ is a decimal number, I would like to round it to 1.7.
Another difficulty is that $integerB$ is an integer in fact,
and should be printed as 1.
end{document}
Note that I used pgfmathprintnumber
to round to one digit after the dot. Of course, you can remove the print
s.
edited Dec 31 '18 at 18:20
answered Dec 31 '18 at 17:50
![](https://i.stack.imgur.com/5vfbM.png?s=32&g=1)
![](https://i.stack.imgur.com/5vfbM.png?s=32&g=1)
marmotmarmot
99.5k4115220
99.5k4115220
add a comment |
add a comment |
Here's a LuaLaTeX-based solution. integer
(4/2) and integerB
((1/3)*3) evaluate to integers automatically according to Lua rules. The LaTeX macro round
, which takes two arguments, lets users round numbers to a specified set of digits after the decimal marker.
% !TEX TS-program = lualatex
documentclass{article}
% Set up 2 auxilliary Lua functions to round numbers
directlua{
function math.round_int ( x )
return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
end
function math.round ( x , n )
return ( math.round_int ( x*10^n ) / 10^n )
end
}
newcommandcomp[1]{directlua{tex.sprint(#1)}}
newcommandround[2]{directlua{tex.sprint(math.round(#1,#2))}}
newcommand{integer}{comp{4/2}}
newcommand{decimal}{comp{5/3}}
newcommand{integerB}{comp{(1/3)*3}}
begin{document}
integer is an integer.
integerB is also an integer.
decimal is a decimal number. I would like to round it to round{decimal}{1}.
end{document}
1
does(1/7-1/8-1/57)*3192
evaluate to an integer in Lua? (just curious...)
– user4686
Jan 1 at 13:37
1
in case answer is yes (perhaps from some distributivity done automatically), I have(1/7 - 1/8 - 1/57)*(3000+192)
up my sleeve :)
– user4686
Jan 1 at 13:43
@jfbu - Both expressions evaluate to0.99999999999998
(13 nines followed by an 8), using the standard number of significant digits used intex.sprint
. If I reduced that number by 1 or 2 digits, one is back to1
(pun intended).
– Mico
Jan 1 at 13:46
2
I thinkxintthefloatexpr
works withxintDigits:=2;
but I am not sure with only 1 digit :) you are really quite a challenge!
– user4686
Jan 1 at 13:53
add a comment |
Here's a LuaLaTeX-based solution. integer
(4/2) and integerB
((1/3)*3) evaluate to integers automatically according to Lua rules. The LaTeX macro round
, which takes two arguments, lets users round numbers to a specified set of digits after the decimal marker.
% !TEX TS-program = lualatex
documentclass{article}
% Set up 2 auxilliary Lua functions to round numbers
directlua{
function math.round_int ( x )
return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
end
function math.round ( x , n )
return ( math.round_int ( x*10^n ) / 10^n )
end
}
newcommandcomp[1]{directlua{tex.sprint(#1)}}
newcommandround[2]{directlua{tex.sprint(math.round(#1,#2))}}
newcommand{integer}{comp{4/2}}
newcommand{decimal}{comp{5/3}}
newcommand{integerB}{comp{(1/3)*3}}
begin{document}
integer is an integer.
integerB is also an integer.
decimal is a decimal number. I would like to round it to round{decimal}{1}.
end{document}
1
does(1/7-1/8-1/57)*3192
evaluate to an integer in Lua? (just curious...)
– user4686
Jan 1 at 13:37
1
in case answer is yes (perhaps from some distributivity done automatically), I have(1/7 - 1/8 - 1/57)*(3000+192)
up my sleeve :)
– user4686
Jan 1 at 13:43
@jfbu - Both expressions evaluate to0.99999999999998
(13 nines followed by an 8), using the standard number of significant digits used intex.sprint
. If I reduced that number by 1 or 2 digits, one is back to1
(pun intended).
– Mico
Jan 1 at 13:46
2
I thinkxintthefloatexpr
works withxintDigits:=2;
but I am not sure with only 1 digit :) you are really quite a challenge!
– user4686
Jan 1 at 13:53
add a comment |
Here's a LuaLaTeX-based solution. integer
(4/2) and integerB
((1/3)*3) evaluate to integers automatically according to Lua rules. The LaTeX macro round
, which takes two arguments, lets users round numbers to a specified set of digits after the decimal marker.
% !TEX TS-program = lualatex
documentclass{article}
% Set up 2 auxilliary Lua functions to round numbers
directlua{
function math.round_int ( x )
return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
end
function math.round ( x , n )
return ( math.round_int ( x*10^n ) / 10^n )
end
}
newcommandcomp[1]{directlua{tex.sprint(#1)}}
newcommandround[2]{directlua{tex.sprint(math.round(#1,#2))}}
newcommand{integer}{comp{4/2}}
newcommand{decimal}{comp{5/3}}
newcommand{integerB}{comp{(1/3)*3}}
begin{document}
integer is an integer.
integerB is also an integer.
decimal is a decimal number. I would like to round it to round{decimal}{1}.
end{document}
Here's a LuaLaTeX-based solution. integer
(4/2) and integerB
((1/3)*3) evaluate to integers automatically according to Lua rules. The LaTeX macro round
, which takes two arguments, lets users round numbers to a specified set of digits after the decimal marker.
% !TEX TS-program = lualatex
documentclass{article}
% Set up 2 auxilliary Lua functions to round numbers
directlua{
function math.round_int ( x )
return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
end
function math.round ( x , n )
return ( math.round_int ( x*10^n ) / 10^n )
end
}
newcommandcomp[1]{directlua{tex.sprint(#1)}}
newcommandround[2]{directlua{tex.sprint(math.round(#1,#2))}}
newcommand{integer}{comp{4/2}}
newcommand{decimal}{comp{5/3}}
newcommand{integerB}{comp{(1/3)*3}}
begin{document}
integer is an integer.
integerB is also an integer.
decimal is a decimal number. I would like to round it to round{decimal}{1}.
end{document}
answered Jan 1 at 12:59
![](https://i.stack.imgur.com/c7PDj.jpg?s=32&g=1)
![](https://i.stack.imgur.com/c7PDj.jpg?s=32&g=1)
MicoMico
278k30380768
278k30380768
1
does(1/7-1/8-1/57)*3192
evaluate to an integer in Lua? (just curious...)
– user4686
Jan 1 at 13:37
1
in case answer is yes (perhaps from some distributivity done automatically), I have(1/7 - 1/8 - 1/57)*(3000+192)
up my sleeve :)
– user4686
Jan 1 at 13:43
@jfbu - Both expressions evaluate to0.99999999999998
(13 nines followed by an 8), using the standard number of significant digits used intex.sprint
. If I reduced that number by 1 or 2 digits, one is back to1
(pun intended).
– Mico
Jan 1 at 13:46
2
I thinkxintthefloatexpr
works withxintDigits:=2;
but I am not sure with only 1 digit :) you are really quite a challenge!
– user4686
Jan 1 at 13:53
add a comment |
1
does(1/7-1/8-1/57)*3192
evaluate to an integer in Lua? (just curious...)
– user4686
Jan 1 at 13:37
1
in case answer is yes (perhaps from some distributivity done automatically), I have(1/7 - 1/8 - 1/57)*(3000+192)
up my sleeve :)
– user4686
Jan 1 at 13:43
@jfbu - Both expressions evaluate to0.99999999999998
(13 nines followed by an 8), using the standard number of significant digits used intex.sprint
. If I reduced that number by 1 or 2 digits, one is back to1
(pun intended).
– Mico
Jan 1 at 13:46
2
I thinkxintthefloatexpr
works withxintDigits:=2;
but I am not sure with only 1 digit :) you are really quite a challenge!
– user4686
Jan 1 at 13:53
1
1
does
(1/7-1/8-1/57)*3192
evaluate to an integer in Lua? (just curious...)– user4686
Jan 1 at 13:37
does
(1/7-1/8-1/57)*3192
evaluate to an integer in Lua? (just curious...)– user4686
Jan 1 at 13:37
1
1
in case answer is yes (perhaps from some distributivity done automatically), I have
(1/7 - 1/8 - 1/57)*(3000+192)
up my sleeve :)– user4686
Jan 1 at 13:43
in case answer is yes (perhaps from some distributivity done automatically), I have
(1/7 - 1/8 - 1/57)*(3000+192)
up my sleeve :)– user4686
Jan 1 at 13:43
@jfbu - Both expressions evaluate to
0.99999999999998
(13 nines followed by an 8), using the standard number of significant digits used in tex.sprint
. If I reduced that number by 1 or 2 digits, one is back to 1
(pun intended).– Mico
Jan 1 at 13:46
@jfbu - Both expressions evaluate to
0.99999999999998
(13 nines followed by an 8), using the standard number of significant digits used in tex.sprint
. If I reduced that number by 1 or 2 digits, one is back to 1
(pun intended).– Mico
Jan 1 at 13:46
2
2
I think
xintthefloatexpr
works with xintDigits:=2;
but I am not sure with only 1 digit :) you are really quite a challenge!– user4686
Jan 1 at 13:53
I think
xintthefloatexpr
works with xintDigits:=2;
but I am not sure with only 1 digit :) you are really quite a challenge!– user4686
Jan 1 at 13:53
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f468046%2ftest-of-integers-or-round-the-number-if-the-first-two-decimal-numbers-are-suff%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
eZGT09a9X KmCtwD7pDMZetUl4s 0O4 X,xqPu5R1fh5Tyh,rzHDsjiZICj,4OLoO,m