How to prevent mathematica rounding extremely small numbers to zero?
$begingroup$
I have a function that, while the maths itself is unimportant, at certain values it results in a very large number multiplying a very small number. E.g. 10^450000 * 10^-449998. As you can see, this should output the more-sensible number 10^2. However Mathematica is rounding the small number to zero thus the whole calculation breaks down. How can I prevent this? I've played with MinNumber and MachinePrecision but neither seem to fix the issue.
Thanks in advance!
Edit: Including the equation as requested:
$exp[cfrac{omega^2}{sigma^2}] BesselK[1,cfrac{sqrt(cfrac{omega}{sigma^2})}{sqrt(cfrac{sigma^2}{omega^3})}]$
Equation breaks down for $sigma<0.01*omega$ and generally unreliable below $sigma<0.04*omega$
Edit2: And the Mathematica code!
bessktot[ω0_, σ_] := BesselK[1, Sqrt[ω0/σ^2]/Sqrt[σ^2/ω0^3]]
expcalctot[ω0_, σ_] := E^(ω0^2/σ^2);
ω0 = 2 [Pi] 10^12;
σ[BWpc_] := BWpc/100 ω0;
σt = σ[1]
bessktot[ω0, σt]
expcalctot[ω0, σt]
expcalctot[ω0, σt]*bessktot[ω0, σt]
Edit3: Thanks Bob Hanlon, (I can't comment back on your answer yet). Your answer works for certain values input to the bessel. The problem appears to be even more fundamental than I realised. It appears Mathematica can't calculate non-integer $BesselK[1,x]$ functions when $x>741$. Is there a way around this?
equation-solving numerics evaluation
New contributor
$endgroup$
|
show 5 more comments
$begingroup$
I have a function that, while the maths itself is unimportant, at certain values it results in a very large number multiplying a very small number. E.g. 10^450000 * 10^-449998. As you can see, this should output the more-sensible number 10^2. However Mathematica is rounding the small number to zero thus the whole calculation breaks down. How can I prevent this? I've played with MinNumber and MachinePrecision but neither seem to fix the issue.
Thanks in advance!
Edit: Including the equation as requested:
$exp[cfrac{omega^2}{sigma^2}] BesselK[1,cfrac{sqrt(cfrac{omega}{sigma^2})}{sqrt(cfrac{sigma^2}{omega^3})}]$
Equation breaks down for $sigma<0.01*omega$ and generally unreliable below $sigma<0.04*omega$
Edit2: And the Mathematica code!
bessktot[ω0_, σ_] := BesselK[1, Sqrt[ω0/σ^2]/Sqrt[σ^2/ω0^3]]
expcalctot[ω0_, σ_] := E^(ω0^2/σ^2);
ω0 = 2 [Pi] 10^12;
σ[BWpc_] := BWpc/100 ω0;
σt = σ[1]
bessktot[ω0, σt]
expcalctot[ω0, σt]
expcalctot[ω0, σt]*bessktot[ω0, σt]
Edit3: Thanks Bob Hanlon, (I can't comment back on your answer yet). Your answer works for certain values input to the bessel. The problem appears to be even more fundamental than I realised. It appears Mathematica can't calculate non-integer $BesselK[1,x]$ functions when $x>741$. Is there a way around this?
equation-solving numerics evaluation
New contributor
$endgroup$
$begingroup$
On my machine, even with machine precision exponents,10^45000000000000.*10^-44999999999998.
outputs1.0 * 10^2
from a fresh startup. It should only start rounding to 0 if it runs out of precision during the calculation, one of the intermediate functions does not handle arbitrary precision arithmetic (not common, but some don't), if it's explicitly told to start rounding somewhere, or if something is actually multiplied by 0. We will need a bit more code to diagnose what's actually going on, so please consider posting your function.
$endgroup$
– eyorble
yesterday
$begingroup$
@eyorblade0^45000000000000.
is certainly no a machine precision number:Precision[110^45000000000000.]
.
$endgroup$
– Henrik Schumacher
yesterday
$begingroup$
@HenrikSchumacher Ina_^b_
, theb
is machine precision, is it not? The resulting value is not, obviously, because there's any precision tracking at all. I suspect Mathematica knows what it's doing here, in that with exacta
the precision isn't lost very quickly at all, but the exponent isn't the source of the uncertainty in this case.
$endgroup$
– eyorble
yesterday
$begingroup$
If the issue is with Mathematica code, please post the Mathematica code rather than a LaTeX representation.
$endgroup$
– JimB
yesterday
2
$begingroup$
@Andrew When I approved your edit, I notice that you have two different accounts, both with the same name. I do not know how that happened, but you should be able to combine the two. Then, you will not need the approval of others to edit your own question.
$endgroup$
– bbgodfrey
yesterday
|
show 5 more comments
$begingroup$
I have a function that, while the maths itself is unimportant, at certain values it results in a very large number multiplying a very small number. E.g. 10^450000 * 10^-449998. As you can see, this should output the more-sensible number 10^2. However Mathematica is rounding the small number to zero thus the whole calculation breaks down. How can I prevent this? I've played with MinNumber and MachinePrecision but neither seem to fix the issue.
Thanks in advance!
Edit: Including the equation as requested:
$exp[cfrac{omega^2}{sigma^2}] BesselK[1,cfrac{sqrt(cfrac{omega}{sigma^2})}{sqrt(cfrac{sigma^2}{omega^3})}]$
Equation breaks down for $sigma<0.01*omega$ and generally unreliable below $sigma<0.04*omega$
Edit2: And the Mathematica code!
bessktot[ω0_, σ_] := BesselK[1, Sqrt[ω0/σ^2]/Sqrt[σ^2/ω0^3]]
expcalctot[ω0_, σ_] := E^(ω0^2/σ^2);
ω0 = 2 [Pi] 10^12;
σ[BWpc_] := BWpc/100 ω0;
σt = σ[1]
bessktot[ω0, σt]
expcalctot[ω0, σt]
expcalctot[ω0, σt]*bessktot[ω0, σt]
Edit3: Thanks Bob Hanlon, (I can't comment back on your answer yet). Your answer works for certain values input to the bessel. The problem appears to be even more fundamental than I realised. It appears Mathematica can't calculate non-integer $BesselK[1,x]$ functions when $x>741$. Is there a way around this?
equation-solving numerics evaluation
New contributor
$endgroup$
I have a function that, while the maths itself is unimportant, at certain values it results in a very large number multiplying a very small number. E.g. 10^450000 * 10^-449998. As you can see, this should output the more-sensible number 10^2. However Mathematica is rounding the small number to zero thus the whole calculation breaks down. How can I prevent this? I've played with MinNumber and MachinePrecision but neither seem to fix the issue.
Thanks in advance!
Edit: Including the equation as requested:
$exp[cfrac{omega^2}{sigma^2}] BesselK[1,cfrac{sqrt(cfrac{omega}{sigma^2})}{sqrt(cfrac{sigma^2}{omega^3})}]$
Equation breaks down for $sigma<0.01*omega$ and generally unreliable below $sigma<0.04*omega$
Edit2: And the Mathematica code!
bessktot[ω0_, σ_] := BesselK[1, Sqrt[ω0/σ^2]/Sqrt[σ^2/ω0^3]]
expcalctot[ω0_, σ_] := E^(ω0^2/σ^2);
ω0 = 2 [Pi] 10^12;
σ[BWpc_] := BWpc/100 ω0;
σt = σ[1]
bessktot[ω0, σt]
expcalctot[ω0, σt]
expcalctot[ω0, σt]*bessktot[ω0, σt]
Edit3: Thanks Bob Hanlon, (I can't comment back on your answer yet). Your answer works for certain values input to the bessel. The problem appears to be even more fundamental than I realised. It appears Mathematica can't calculate non-integer $BesselK[1,x]$ functions when $x>741$. Is there a way around this?
equation-solving numerics evaluation
equation-solving numerics evaluation
New contributor
New contributor
edited yesterday
Andrew
32
32
New contributor
asked yesterday
AndrewAndrew
212
212
New contributor
New contributor
$begingroup$
On my machine, even with machine precision exponents,10^45000000000000.*10^-44999999999998.
outputs1.0 * 10^2
from a fresh startup. It should only start rounding to 0 if it runs out of precision during the calculation, one of the intermediate functions does not handle arbitrary precision arithmetic (not common, but some don't), if it's explicitly told to start rounding somewhere, or if something is actually multiplied by 0. We will need a bit more code to diagnose what's actually going on, so please consider posting your function.
$endgroup$
– eyorble
yesterday
$begingroup$
@eyorblade0^45000000000000.
is certainly no a machine precision number:Precision[110^45000000000000.]
.
$endgroup$
– Henrik Schumacher
yesterday
$begingroup$
@HenrikSchumacher Ina_^b_
, theb
is machine precision, is it not? The resulting value is not, obviously, because there's any precision tracking at all. I suspect Mathematica knows what it's doing here, in that with exacta
the precision isn't lost very quickly at all, but the exponent isn't the source of the uncertainty in this case.
$endgroup$
– eyorble
yesterday
$begingroup$
If the issue is with Mathematica code, please post the Mathematica code rather than a LaTeX representation.
$endgroup$
– JimB
yesterday
2
$begingroup$
@Andrew When I approved your edit, I notice that you have two different accounts, both with the same name. I do not know how that happened, but you should be able to combine the two. Then, you will not need the approval of others to edit your own question.
$endgroup$
– bbgodfrey
yesterday
|
show 5 more comments
$begingroup$
On my machine, even with machine precision exponents,10^45000000000000.*10^-44999999999998.
outputs1.0 * 10^2
from a fresh startup. It should only start rounding to 0 if it runs out of precision during the calculation, one of the intermediate functions does not handle arbitrary precision arithmetic (not common, but some don't), if it's explicitly told to start rounding somewhere, or if something is actually multiplied by 0. We will need a bit more code to diagnose what's actually going on, so please consider posting your function.
$endgroup$
– eyorble
yesterday
$begingroup$
@eyorblade0^45000000000000.
is certainly no a machine precision number:Precision[110^45000000000000.]
.
$endgroup$
– Henrik Schumacher
yesterday
$begingroup$
@HenrikSchumacher Ina_^b_
, theb
is machine precision, is it not? The resulting value is not, obviously, because there's any precision tracking at all. I suspect Mathematica knows what it's doing here, in that with exacta
the precision isn't lost very quickly at all, but the exponent isn't the source of the uncertainty in this case.
$endgroup$
– eyorble
yesterday
$begingroup$
If the issue is with Mathematica code, please post the Mathematica code rather than a LaTeX representation.
$endgroup$
– JimB
yesterday
2
$begingroup$
@Andrew When I approved your edit, I notice that you have two different accounts, both with the same name. I do not know how that happened, but you should be able to combine the two. Then, you will not need the approval of others to edit your own question.
$endgroup$
– bbgodfrey
yesterday
$begingroup$
On my machine, even with machine precision exponents,
10^45000000000000.*10^-44999999999998.
outputs 1.0 * 10^2
from a fresh startup. It should only start rounding to 0 if it runs out of precision during the calculation, one of the intermediate functions does not handle arbitrary precision arithmetic (not common, but some don't), if it's explicitly told to start rounding somewhere, or if something is actually multiplied by 0. We will need a bit more code to diagnose what's actually going on, so please consider posting your function.$endgroup$
– eyorble
yesterday
$begingroup$
On my machine, even with machine precision exponents,
10^45000000000000.*10^-44999999999998.
outputs 1.0 * 10^2
from a fresh startup. It should only start rounding to 0 if it runs out of precision during the calculation, one of the intermediate functions does not handle arbitrary precision arithmetic (not common, but some don't), if it's explicitly told to start rounding somewhere, or if something is actually multiplied by 0. We will need a bit more code to diagnose what's actually going on, so please consider posting your function.$endgroup$
– eyorble
yesterday
$begingroup$
@eyorblade
0^45000000000000.
is certainly no a machine precision number: Precision[110^45000000000000.]
.$endgroup$
– Henrik Schumacher
yesterday
$begingroup$
@eyorblade
0^45000000000000.
is certainly no a machine precision number: Precision[110^45000000000000.]
.$endgroup$
– Henrik Schumacher
yesterday
$begingroup$
@HenrikSchumacher In
a_^b_
, the b
is machine precision, is it not? The resulting value is not, obviously, because there's any precision tracking at all. I suspect Mathematica knows what it's doing here, in that with exact a
the precision isn't lost very quickly at all, but the exponent isn't the source of the uncertainty in this case.$endgroup$
– eyorble
yesterday
$begingroup$
@HenrikSchumacher In
a_^b_
, the b
is machine precision, is it not? The resulting value is not, obviously, because there's any precision tracking at all. I suspect Mathematica knows what it's doing here, in that with exact a
the precision isn't lost very quickly at all, but the exponent isn't the source of the uncertainty in this case.$endgroup$
– eyorble
yesterday
$begingroup$
If the issue is with Mathematica code, please post the Mathematica code rather than a LaTeX representation.
$endgroup$
– JimB
yesterday
$begingroup$
If the issue is with Mathematica code, please post the Mathematica code rather than a LaTeX representation.
$endgroup$
– JimB
yesterday
2
2
$begingroup$
@Andrew When I approved your edit, I notice that you have two different accounts, both with the same name. I do not know how that happened, but you should be able to combine the two. Then, you will not need the approval of others to edit your own question.
$endgroup$
– bbgodfrey
yesterday
$begingroup$
@Andrew When I approved your edit, I notice that you have two different accounts, both with the same name. I do not know how that happened, but you should be able to combine the two. Then, you will not need the approval of others to edit your own question.
$endgroup$
– bbgodfrey
yesterday
|
show 5 more comments
1 Answer
1
active
oldest
votes
$begingroup$
There was a change in handling underflow in later versions.
$Version
(* "11.3.0 for Mac OS X x86 (64-bit) (March 7, 2018)" *)
Clear["Global`*"]
bessktot[ω0_, σ_] :=
BesselK[1, Sqrt[ω0/σ^2]/Sqrt[σ^2/ω0^3]];
expcalctot[ω0_, σ_] := E^(ω0^2/σ^2);
ω0 = 2 π 10^12;
σ[BWpc_] := BWpc/100 ω0;
σt = σ[1];
MachinePrecision is insufficient,
(expr = expcalctot[ω0, σt]*bessktot[ω0, σt]) // N
(* 0. *)
Use arbitrary precision
expr // N[#, $MachinePrecision] &
(* 0.01253361135127051 *)
expr // N[#, 20] &
(* 0.012533611351270505734 *)
EDIT: For large input to BesselK
you need to control the precision of the input.
BesselK[1, #] & /@ {801., 801.`20, SetPrecision[801., 20]}
(* {0., 5.9781508629496523*10^-350, 5.9781508629496523*10^-350} *)
$endgroup$
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
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
});
}
});
Andrew is a new contributor. Be nice, and check out our Code of Conduct.
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%2fmathematica.stackexchange.com%2fquestions%2f195302%2fhow-to-prevent-mathematica-rounding-extremely-small-numbers-to-zero%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
There was a change in handling underflow in later versions.
$Version
(* "11.3.0 for Mac OS X x86 (64-bit) (March 7, 2018)" *)
Clear["Global`*"]
bessktot[ω0_, σ_] :=
BesselK[1, Sqrt[ω0/σ^2]/Sqrt[σ^2/ω0^3]];
expcalctot[ω0_, σ_] := E^(ω0^2/σ^2);
ω0 = 2 π 10^12;
σ[BWpc_] := BWpc/100 ω0;
σt = σ[1];
MachinePrecision is insufficient,
(expr = expcalctot[ω0, σt]*bessktot[ω0, σt]) // N
(* 0. *)
Use arbitrary precision
expr // N[#, $MachinePrecision] &
(* 0.01253361135127051 *)
expr // N[#, 20] &
(* 0.012533611351270505734 *)
EDIT: For large input to BesselK
you need to control the precision of the input.
BesselK[1, #] & /@ {801., 801.`20, SetPrecision[801., 20]}
(* {0., 5.9781508629496523*10^-350, 5.9781508629496523*10^-350} *)
$endgroup$
add a comment |
$begingroup$
There was a change in handling underflow in later versions.
$Version
(* "11.3.0 for Mac OS X x86 (64-bit) (March 7, 2018)" *)
Clear["Global`*"]
bessktot[ω0_, σ_] :=
BesselK[1, Sqrt[ω0/σ^2]/Sqrt[σ^2/ω0^3]];
expcalctot[ω0_, σ_] := E^(ω0^2/σ^2);
ω0 = 2 π 10^12;
σ[BWpc_] := BWpc/100 ω0;
σt = σ[1];
MachinePrecision is insufficient,
(expr = expcalctot[ω0, σt]*bessktot[ω0, σt]) // N
(* 0. *)
Use arbitrary precision
expr // N[#, $MachinePrecision] &
(* 0.01253361135127051 *)
expr // N[#, 20] &
(* 0.012533611351270505734 *)
EDIT: For large input to BesselK
you need to control the precision of the input.
BesselK[1, #] & /@ {801., 801.`20, SetPrecision[801., 20]}
(* {0., 5.9781508629496523*10^-350, 5.9781508629496523*10^-350} *)
$endgroup$
add a comment |
$begingroup$
There was a change in handling underflow in later versions.
$Version
(* "11.3.0 for Mac OS X x86 (64-bit) (March 7, 2018)" *)
Clear["Global`*"]
bessktot[ω0_, σ_] :=
BesselK[1, Sqrt[ω0/σ^2]/Sqrt[σ^2/ω0^3]];
expcalctot[ω0_, σ_] := E^(ω0^2/σ^2);
ω0 = 2 π 10^12;
σ[BWpc_] := BWpc/100 ω0;
σt = σ[1];
MachinePrecision is insufficient,
(expr = expcalctot[ω0, σt]*bessktot[ω0, σt]) // N
(* 0. *)
Use arbitrary precision
expr // N[#, $MachinePrecision] &
(* 0.01253361135127051 *)
expr // N[#, 20] &
(* 0.012533611351270505734 *)
EDIT: For large input to BesselK
you need to control the precision of the input.
BesselK[1, #] & /@ {801., 801.`20, SetPrecision[801., 20]}
(* {0., 5.9781508629496523*10^-350, 5.9781508629496523*10^-350} *)
$endgroup$
There was a change in handling underflow in later versions.
$Version
(* "11.3.0 for Mac OS X x86 (64-bit) (March 7, 2018)" *)
Clear["Global`*"]
bessktot[ω0_, σ_] :=
BesselK[1, Sqrt[ω0/σ^2]/Sqrt[σ^2/ω0^3]];
expcalctot[ω0_, σ_] := E^(ω0^2/σ^2);
ω0 = 2 π 10^12;
σ[BWpc_] := BWpc/100 ω0;
σt = σ[1];
MachinePrecision is insufficient,
(expr = expcalctot[ω0, σt]*bessktot[ω0, σt]) // N
(* 0. *)
Use arbitrary precision
expr // N[#, $MachinePrecision] &
(* 0.01253361135127051 *)
expr // N[#, 20] &
(* 0.012533611351270505734 *)
EDIT: For large input to BesselK
you need to control the precision of the input.
BesselK[1, #] & /@ {801., 801.`20, SetPrecision[801., 20]}
(* {0., 5.9781508629496523*10^-350, 5.9781508629496523*10^-350} *)
edited yesterday
answered yesterday
Bob HanlonBob Hanlon
61.7k33598
61.7k33598
add a comment |
add a comment |
Andrew is a new contributor. Be nice, and check out our Code of Conduct.
Andrew is a new contributor. Be nice, and check out our Code of Conduct.
Andrew is a new contributor. Be nice, and check out our Code of Conduct.
Andrew is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Mathematica 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.
Use MathJax to format equations. MathJax reference.
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%2fmathematica.stackexchange.com%2fquestions%2f195302%2fhow-to-prevent-mathematica-rounding-extremely-small-numbers-to-zero%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
$begingroup$
On my machine, even with machine precision exponents,
10^45000000000000.*10^-44999999999998.
outputs1.0 * 10^2
from a fresh startup. It should only start rounding to 0 if it runs out of precision during the calculation, one of the intermediate functions does not handle arbitrary precision arithmetic (not common, but some don't), if it's explicitly told to start rounding somewhere, or if something is actually multiplied by 0. We will need a bit more code to diagnose what's actually going on, so please consider posting your function.$endgroup$
– eyorble
yesterday
$begingroup$
@eyorblade
0^45000000000000.
is certainly no a machine precision number:Precision[110^45000000000000.]
.$endgroup$
– Henrik Schumacher
yesterday
$begingroup$
@HenrikSchumacher In
a_^b_
, theb
is machine precision, is it not? The resulting value is not, obviously, because there's any precision tracking at all. I suspect Mathematica knows what it's doing here, in that with exacta
the precision isn't lost very quickly at all, but the exponent isn't the source of the uncertainty in this case.$endgroup$
– eyorble
yesterday
$begingroup$
If the issue is with Mathematica code, please post the Mathematica code rather than a LaTeX representation.
$endgroup$
– JimB
yesterday
2
$begingroup$
@Andrew When I approved your edit, I notice that you have two different accounts, both with the same name. I do not know how that happened, but you should be able to combine the two. Then, you will not need the approval of others to edit your own question.
$endgroup$
– bbgodfrey
yesterday