divide by zero encountered in true_divide + invalid value encountered in true_divide + invalid value...
Here is what I've tried to plot a semi-Planck law based on the variations of temperature and distance as follows.
import numpy as np
k = 1.381*np.power(10,-23, dtype=np.float)
c = 3*np.power(10,8)
h = 6.626*np.power(10,-34, dtype=np.float)
l = 3*np.power(10,-6, dtype=np.float)
d_lower = 16*np.power(10,4)
d_upper = 2*np.power(10,6)
t_lower = 740
t_upper = 5200
d = np.arange(d_lower,d_upper,100)
t = np.arange(t_lower,t_upper,10)
D,T = np.meshgrid(d, t)
I = (2*h*np.power(c,2))/(np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))
The interpreter returns the following error:
RuntimeWarning: divide by zero encountered in true_divide
I = (2*h*np.power(c,2))/(np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))
I shouldn't have been encountered any divide-by-zero since the T values are in Kelvin, so np.exp((h*c)/(l*k*T))-1 can't become zero.
What is wrong here?!
My python and numpy versions are 3.7.0 and 1.15.4, respectively.
python numpy divide-by-zero
|
show 5 more comments
Here is what I've tried to plot a semi-Planck law based on the variations of temperature and distance as follows.
import numpy as np
k = 1.381*np.power(10,-23, dtype=np.float)
c = 3*np.power(10,8)
h = 6.626*np.power(10,-34, dtype=np.float)
l = 3*np.power(10,-6, dtype=np.float)
d_lower = 16*np.power(10,4)
d_upper = 2*np.power(10,6)
t_lower = 740
t_upper = 5200
d = np.arange(d_lower,d_upper,100)
t = np.arange(t_lower,t_upper,10)
D,T = np.meshgrid(d, t)
I = (2*h*np.power(c,2))/(np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))
The interpreter returns the following error:
RuntimeWarning: divide by zero encountered in true_divide
I = (2*h*np.power(c,2))/(np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))
I shouldn't have been encountered any divide-by-zero since the T values are in Kelvin, so np.exp((h*c)/(l*k*T))-1 can't become zero.
What is wrong here?!
My python and numpy versions are 3.7.0 and 1.15.4, respectively.
python numpy divide-by-zero
The code works fine for me without any warnings or error producing a different plot (not sure if it is what you want). I am using matplotlib 2.2.2 and python 3.6.5. What version do you use?
– Bazingaa
Jan 18 at 14:01
@Bazingaa: My python is3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)], and my matplotlib is3.0.2.
– Pinton
Jan 18 at 14:06
This is what I am getting
– Bazingaa
Jan 18 at 14:13
2
The problem seems to be solved by using a float array,d = np.arange(d_lower,d_upper,100.)(d.dtype == float64) instead of an integer arrayd = np.arange(d_lower,d_upper,100)(d.dtype == int32). I currently don't know why that causes the issue.
– ImportanceOfBeingErnest
Jan 18 at 15:04
1
Presumably you are on a platform (probably Windows) where the default data type for a numpy integer array is 32 bit (np.int32). The expressionsnp.power(D, 2)is computed using the data type ofD, so its result has typenp.int32. However, the exact values in that result are larger than can be represented with 32 bit integers, so the values overflow and may become negative or zero. For example,np.array([106000, 1638400], dtype=np.int32)**2returnsarray([-1648901888, 0], dtype=int32). @ImportanceOfBeingErnest's suggestion to use floating point will fix the problem.
– Warren Weckesser
Jan 18 at 15:55
|
show 5 more comments
Here is what I've tried to plot a semi-Planck law based on the variations of temperature and distance as follows.
import numpy as np
k = 1.381*np.power(10,-23, dtype=np.float)
c = 3*np.power(10,8)
h = 6.626*np.power(10,-34, dtype=np.float)
l = 3*np.power(10,-6, dtype=np.float)
d_lower = 16*np.power(10,4)
d_upper = 2*np.power(10,6)
t_lower = 740
t_upper = 5200
d = np.arange(d_lower,d_upper,100)
t = np.arange(t_lower,t_upper,10)
D,T = np.meshgrid(d, t)
I = (2*h*np.power(c,2))/(np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))
The interpreter returns the following error:
RuntimeWarning: divide by zero encountered in true_divide
I = (2*h*np.power(c,2))/(np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))
I shouldn't have been encountered any divide-by-zero since the T values are in Kelvin, so np.exp((h*c)/(l*k*T))-1 can't become zero.
What is wrong here?!
My python and numpy versions are 3.7.0 and 1.15.4, respectively.
python numpy divide-by-zero
Here is what I've tried to plot a semi-Planck law based on the variations of temperature and distance as follows.
import numpy as np
k = 1.381*np.power(10,-23, dtype=np.float)
c = 3*np.power(10,8)
h = 6.626*np.power(10,-34, dtype=np.float)
l = 3*np.power(10,-6, dtype=np.float)
d_lower = 16*np.power(10,4)
d_upper = 2*np.power(10,6)
t_lower = 740
t_upper = 5200
d = np.arange(d_lower,d_upper,100)
t = np.arange(t_lower,t_upper,10)
D,T = np.meshgrid(d, t)
I = (2*h*np.power(c,2))/(np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))
The interpreter returns the following error:
RuntimeWarning: divide by zero encountered in true_divide
I = (2*h*np.power(c,2))/(np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))
I shouldn't have been encountered any divide-by-zero since the T values are in Kelvin, so np.exp((h*c)/(l*k*T))-1 can't become zero.
What is wrong here?!
My python and numpy versions are 3.7.0 and 1.15.4, respectively.
python numpy divide-by-zero
python numpy divide-by-zero
edited Jan 18 at 14:57
ImportanceOfBeingErnest
128k12136212
128k12136212
asked Jan 18 at 13:54
PintonPinton
566
566
The code works fine for me without any warnings or error producing a different plot (not sure if it is what you want). I am using matplotlib 2.2.2 and python 3.6.5. What version do you use?
– Bazingaa
Jan 18 at 14:01
@Bazingaa: My python is3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)], and my matplotlib is3.0.2.
– Pinton
Jan 18 at 14:06
This is what I am getting
– Bazingaa
Jan 18 at 14:13
2
The problem seems to be solved by using a float array,d = np.arange(d_lower,d_upper,100.)(d.dtype == float64) instead of an integer arrayd = np.arange(d_lower,d_upper,100)(d.dtype == int32). I currently don't know why that causes the issue.
– ImportanceOfBeingErnest
Jan 18 at 15:04
1
Presumably you are on a platform (probably Windows) where the default data type for a numpy integer array is 32 bit (np.int32). The expressionsnp.power(D, 2)is computed using the data type ofD, so its result has typenp.int32. However, the exact values in that result are larger than can be represented with 32 bit integers, so the values overflow and may become negative or zero. For example,np.array([106000, 1638400], dtype=np.int32)**2returnsarray([-1648901888, 0], dtype=int32). @ImportanceOfBeingErnest's suggestion to use floating point will fix the problem.
– Warren Weckesser
Jan 18 at 15:55
|
show 5 more comments
The code works fine for me without any warnings or error producing a different plot (not sure if it is what you want). I am using matplotlib 2.2.2 and python 3.6.5. What version do you use?
– Bazingaa
Jan 18 at 14:01
@Bazingaa: My python is3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)], and my matplotlib is3.0.2.
– Pinton
Jan 18 at 14:06
This is what I am getting
– Bazingaa
Jan 18 at 14:13
2
The problem seems to be solved by using a float array,d = np.arange(d_lower,d_upper,100.)(d.dtype == float64) instead of an integer arrayd = np.arange(d_lower,d_upper,100)(d.dtype == int32). I currently don't know why that causes the issue.
– ImportanceOfBeingErnest
Jan 18 at 15:04
1
Presumably you are on a platform (probably Windows) where the default data type for a numpy integer array is 32 bit (np.int32). The expressionsnp.power(D, 2)is computed using the data type ofD, so its result has typenp.int32. However, the exact values in that result are larger than can be represented with 32 bit integers, so the values overflow and may become negative or zero. For example,np.array([106000, 1638400], dtype=np.int32)**2returnsarray([-1648901888, 0], dtype=int32). @ImportanceOfBeingErnest's suggestion to use floating point will fix the problem.
– Warren Weckesser
Jan 18 at 15:55
The code works fine for me without any warnings or error producing a different plot (not sure if it is what you want). I am using matplotlib 2.2.2 and python 3.6.5. What version do you use?
– Bazingaa
Jan 18 at 14:01
The code works fine for me without any warnings or error producing a different plot (not sure if it is what you want). I am using matplotlib 2.2.2 and python 3.6.5. What version do you use?
– Bazingaa
Jan 18 at 14:01
@Bazingaa: My python is
3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)], and my matplotlib is 3.0.2.– Pinton
Jan 18 at 14:06
@Bazingaa: My python is
3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)], and my matplotlib is 3.0.2.– Pinton
Jan 18 at 14:06
This is what I am getting
– Bazingaa
Jan 18 at 14:13
This is what I am getting
– Bazingaa
Jan 18 at 14:13
2
2
The problem seems to be solved by using a float array,
d = np.arange(d_lower,d_upper,100.) (d.dtype == float64) instead of an integer array d = np.arange(d_lower,d_upper,100) (d.dtype == int32). I currently don't know why that causes the issue.– ImportanceOfBeingErnest
Jan 18 at 15:04
The problem seems to be solved by using a float array,
d = np.arange(d_lower,d_upper,100.) (d.dtype == float64) instead of an integer array d = np.arange(d_lower,d_upper,100) (d.dtype == int32). I currently don't know why that causes the issue.– ImportanceOfBeingErnest
Jan 18 at 15:04
1
1
Presumably you are on a platform (probably Windows) where the default data type for a numpy integer array is 32 bit (
np.int32). The expressions np.power(D, 2) is computed using the data type of D, so its result has type np.int32. However, the exact values in that result are larger than can be represented with 32 bit integers, so the values overflow and may become negative or zero. For example, np.array([106000, 1638400], dtype=np.int32)**2 returns array([-1648901888, 0], dtype=int32). @ImportanceOfBeingErnest's suggestion to use floating point will fix the problem.– Warren Weckesser
Jan 18 at 15:55
Presumably you are on a platform (probably Windows) where the default data type for a numpy integer array is 32 bit (
np.int32). The expressions np.power(D, 2) is computed using the data type of D, so its result has type np.int32. However, the exact values in that result are larger than can be represented with 32 bit integers, so the values overflow and may become negative or zero. For example, np.array([106000, 1638400], dtype=np.int32)**2 returns array([-1648901888, 0], dtype=int32). @ImportanceOfBeingErnest's suggestion to use floating point will fix the problem.– Warren Weckesser
Jan 18 at 15:55
|
show 5 more comments
1 Answer
1
active
oldest
votes
The problem comes maybe from the fact that it is a matrix division and python doesn't handle it so well.
Try to replace :
I = (2*h*np.power(c,2))/(np.power(D,2)np.power(l,5)(np.exp((hc)/(lk*T))-1))
by :
aaa = (2*h*np.power(c,2))
bbb = (np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))
I = aaa/bbb
It worked for me
SLP
New contributor
user9965855 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Did you read the comments below the question?
– ImportanceOfBeingErnest
Jan 18 at 17:14
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f54255454%2fdivide-by-zero-encountered-in-true-divide-invalid-value-encountered-in-true-di%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
The problem comes maybe from the fact that it is a matrix division and python doesn't handle it so well.
Try to replace :
I = (2*h*np.power(c,2))/(np.power(D,2)np.power(l,5)(np.exp((hc)/(lk*T))-1))
by :
aaa = (2*h*np.power(c,2))
bbb = (np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))
I = aaa/bbb
It worked for me
SLP
New contributor
user9965855 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Did you read the comments below the question?
– ImportanceOfBeingErnest
Jan 18 at 17:14
add a comment |
The problem comes maybe from the fact that it is a matrix division and python doesn't handle it so well.
Try to replace :
I = (2*h*np.power(c,2))/(np.power(D,2)np.power(l,5)(np.exp((hc)/(lk*T))-1))
by :
aaa = (2*h*np.power(c,2))
bbb = (np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))
I = aaa/bbb
It worked for me
SLP
New contributor
user9965855 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Did you read the comments below the question?
– ImportanceOfBeingErnest
Jan 18 at 17:14
add a comment |
The problem comes maybe from the fact that it is a matrix division and python doesn't handle it so well.
Try to replace :
I = (2*h*np.power(c,2))/(np.power(D,2)np.power(l,5)(np.exp((hc)/(lk*T))-1))
by :
aaa = (2*h*np.power(c,2))
bbb = (np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))
I = aaa/bbb
It worked for me
SLP
New contributor
user9965855 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
The problem comes maybe from the fact that it is a matrix division and python doesn't handle it so well.
Try to replace :
I = (2*h*np.power(c,2))/(np.power(D,2)np.power(l,5)(np.exp((hc)/(lk*T))-1))
by :
aaa = (2*h*np.power(c,2))
bbb = (np.power(D,2)*np.power(l,5)*(np.exp((h*c)/(l*k*T))-1))
I = aaa/bbb
It worked for me
SLP
New contributor
user9965855 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user9965855 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Jan 18 at 16:34
user9965855user9965855
1
1
New contributor
user9965855 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user9965855 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
user9965855 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Did you read the comments below the question?
– ImportanceOfBeingErnest
Jan 18 at 17:14
add a comment |
Did you read the comments below the question?
– ImportanceOfBeingErnest
Jan 18 at 17:14
Did you read the comments below the question?
– ImportanceOfBeingErnest
Jan 18 at 17:14
Did you read the comments below the question?
– ImportanceOfBeingErnest
Jan 18 at 17:14
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f54255454%2fdivide-by-zero-encountered-in-true-divide-invalid-value-encountered-in-true-di%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
The code works fine for me without any warnings or error producing a different plot (not sure if it is what you want). I am using matplotlib 2.2.2 and python 3.6.5. What version do you use?
– Bazingaa
Jan 18 at 14:01
@Bazingaa: My python is
3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)], and my matplotlib is3.0.2.– Pinton
Jan 18 at 14:06
This is what I am getting
– Bazingaa
Jan 18 at 14:13
2
The problem seems to be solved by using a float array,
d = np.arange(d_lower,d_upper,100.)(d.dtype == float64) instead of an integer arrayd = np.arange(d_lower,d_upper,100)(d.dtype == int32). I currently don't know why that causes the issue.– ImportanceOfBeingErnest
Jan 18 at 15:04
1
Presumably you are on a platform (probably Windows) where the default data type for a numpy integer array is 32 bit (
np.int32). The expressionsnp.power(D, 2)is computed using the data type ofD, so its result has typenp.int32. However, the exact values in that result are larger than can be represented with 32 bit integers, so the values overflow and may become negative or zero. For example,np.array([106000, 1638400], dtype=np.int32)**2returnsarray([-1648901888, 0], dtype=int32). @ImportanceOfBeingErnest's suggestion to use floating point will fix the problem.– Warren Weckesser
Jan 18 at 15:55