Así que ha escrito un código absurdamente simple, digamos, por ejemplo:
>>> 0.1 + 0.2
y obtuve un resultado realmente inesperado:
>>> 0.1 + 0.2
0.30000000000000004
>>>
Esto se debe a que, internamente, las computadoras usan un formato (punto flotante binario) que no puede representar con precisión un número como 0.1, 0.2 o 0.3 en absoluto.
La matemática de coma flotante binaria es así. En la mayoría de los lenguajes de programación, se basa en el estándar IEEE 754. El meollo del problema es que los números se representan en este formato como un número entero multiplicado por una potencia de dos.
¿Por qué pasó esto?
De hecho, es bastante interesante. Cuando tienes un sistema de base 10 (como el nuestro), solo puede expresar fracciones que usan un factor primo de la base. Los factores primos de 10 son 2 y 5. Entonces 1/2, 1/4, 1/5, 1/8 y 1/10 pueden expresarse claramente porque todos los denominadores usan factores primos de 10. En contraste, 1/3, 1/6, 1/7 y 1/9 son decimales periódicos porque sus denominadores usan un factor primo de 3 o 7.
En binario (o base 2), el único factor primo es 2, por lo que solo puede expresar claramente fracciones cuyo denominador tenga solo 2 como factor primo. En binario, 1/2, 1/4, 1/8 se expresarían claramente como decimales, mientras que 1/5 o 1/10 serían decimales repetidos. Entonces 0.1 y 0.2 (1/10 y 1/5), mientras que los decimales limpios en un sistema de base 10, son decimales repetidos en el sistema de base 2 que usa la computadora. Cuando realiza cálculos matemáticos con estos decimales repetidos, termina con sobras que se transfieren cuando convierte el número de base 2 (binario) de la computadora en una representación de base 10 más legible por humanos.
¿Por qué las computadoras usan un sistema tan raro?
No es raro, solo diferente. Los números decimales no pueden representar con precisión un número como 1/3, por lo que tienes que redondear a algo como 0,33, y tampoco esperas que 0,33 + 0,33 + 0,33 sumen 1, ¿verdad?
Las computadoras usan números binarios porque son más rápidos para manejarlos, y porque para la mayoría de los cálculos, un pequeño error en el decimoséptimo lugar decimal no importa en absoluto, ya que los números con los que trabaja no son redondos (ni tan precisos) de todos modos.
¿Por qué otros cálculos como 0,1 + 0,4 funcionan correctamente?
En ese caso, el resultado (0.5) se puede representar exactamente como un número de punto flotante, y es posible que los errores de redondeo en los números de entrada se cancelen entre sí, pero no necesariamente se puede confiar en eso (por ejemplo, cuando esos dos los números se almacenaron primero en representaciones de punto flotante de diferentes tamaños, es posible que los errores de redondeo no se compensen entre sí).
¿Cómo lo resuelvo?
Para Python podemos usar:
x = .1 + .2
#x = 0.30000000000000004
y = float(decimal.Decimal(‘.1’) + decimal.Decimal(‘.2’))
#y = 0.3
Para Javascript se puede utilizar la librería http://mikemcl.github.io/decimal.js/
I discovered your blog web site on google and test just a few of your early posts. Proceed to maintain up the very good operate. I just additional up your RSS feed to my MSN News Reader. Searching for ahead to studying more from you afterward!…
Your blog is a constant source of inspiration for me. Your passion for your subject matter shines through in every post, and it’s clear that you genuinely care about making a positive impact on your readers.
Hello my family member! I want to say that this article is amazing, nice written and come with approximately all vital infos. I’d like to see extra posts like this .
The next time I learn a weblog, I hope that it doesnt disappoint me as a lot as this one. I imply, I know it was my choice to read, however I actually thought youd have something attention-grabbing to say. All I hear is a bunch of whining about one thing that you could possibly fix for those who werent too busy looking for attention.
I am continuously looking online for ideas that can benefit me. Thx!
It’s really a nice and useful piece of information. I am glad that you shared this useful info with us. Please keep us up to date like this. Thanks for sharing.
أنابيب الألياف الزجاجية والراتنج في العراق تفتخر شركة إيليت بايب في العراق بأنها منتج رائد لأنابيب الألياف الزجاجية والراتنج عالية الجودة، التي توفر أداءً ممتازًا ومتانة لتطبيقات صناعية متنوعة. توفر أنابيب البلاستيك المدعمة بالألياف الزجاجية (FRP)، والمعروفة أيضًا بأنابيب GRP، مقاومة ممتازة للتآكل، وخصائص خفيفة الوزن، وعمر خدمة طويل. تجعل هذه الخصائص منها مثالية للاستخدام في بيئات تتطلب أداءً عالياً مثل معالجة المواد الكيميائية، ومعالجة المياه، وصناعات النفط والغاز. مع التزامنا بالابتكار والجودة، تضمن شركة إيليت بايب أن كل أنبوب يلبي المعايير الصارمة، مما يثبت مكانتنا كواحدة من أفضل وأكثر الموردين موثوقية في العراق. لمزيد من المعلومات، تفضل بزيارة موقعنا على elitepipeiraq.com.
rüyada örgü lif görmek diyanet
Keep up the fantastic work! Kalorifer Sobası odun, kömür, pelet gibi yakıtlarla çalışan ve ısıtma işlevi gören bir soba türüdür. Kalorifer Sobası içindeki yakıtın yanmasıyla oluşan ısıyı doğrudan çevresine yayar ve aynı zamanda suyun ısınmasını sağlar.
Garrett nods to his father. porn sex “But you didn’t though, did you, son?”
He gulps once as his dad mounts more pressure on his balls. Squeezing them. “What are ya now, by the way?”
“I don’t wanna jack-off, dad.” He says flabbergasted to his dad as he turns off the tap to the hot and chilly water in the shower. His father loosens his grip on the full balls of his son as he stands. He can see the spurts of hair that dot the chest of his son. And he can see the heaving and gentle rise and fall of his son’s chest, as he breathes, as his excitement builds.
“Let me get in there, Garrett.” His father says, as his son steps aside so he can get into the shower enclosure. I was surprised He wasn’t angry. He said,” boy you wait here, I am going to the bathroom.” He closed the door. He must sit on the toilet with pants down because I heard him losing the belt but didn’t hear him peeing. After a while, he called my name,” get in here ass sniffing slut.” I opened the door and crawled to the side of daddy. Daddy was still sitting on the toilet. . Just when I was confused why he sat that and did nothing. He stood up and pulled his pants up. I did take a peep of daddy’s huge uncut Middle Eastern dick. Half hard like 7inches already. While he was doing that, he grabbed my hair pushed right into the toilet, and closed the lid. “ smell my ass from there boy when it is still fresh.” He paused and continued, “ Don’t move.”
Do you have to have car insurance in California?
“I don’t wanna jack-off, dad.” He says flabbergasted to his dad as he turns off the tap to the hot and chilly water in the shower.
I do not even know the way I finished up right here, however I thought this submit was good. I do not realize who you are but certainly you are going to a well-known blogger in case you are not already 😉 Cheers!
“Back in the day, back before we turned into a modern society, a man would grab another man’s balls and swear an oath on them in his hand and state his words to be true. To be a friend” His father says. “Do you know what I mean, son?”
great post, very informative. I wonder why the other specialists of this sector do not notice this. You should continue your writing. I am confident, you have a huge readers’ base already!