divide by zero encountered in true_divide + invalid value encountered in true_divide + invalid value...












1















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.










share|improve this question

























  • 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











  • 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 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





    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


















1















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.










share|improve this question

























  • 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











  • 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 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





    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
















1












1








1


1






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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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






  • 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






  • 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





















  • 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











  • 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 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





    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



















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














1 Answer
1






active

oldest

votes


















-1














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






share|improve this answer








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











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
});


}
});














draft saved

draft discarded


















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









-1














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






share|improve this answer








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
















-1














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






share|improve this answer








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














-1












-1








-1







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






share|improve this answer








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







share|improve this answer








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.









share|improve this answer



share|improve this answer






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



















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Callistus III

Plistias Cous

Index Sanctorum