Today I discovered that Rust won't let me use scientific notation on integers. I started with the line let mut num1 = 1*10^9 + 1; expecting the compiler to assign 1,000,000,000,001 to the num1 variable. Instead, it assigned the value 1 and ignored the rest of the line. So, I googled it and found that you have to use "e-notation" when doing exponents. Okay, fine, I changed my line to let mut num1 = 1*10e9 + 1; and that returned lots of error messages. After I did some more checking, I found out that in order to use scientific notation you have to use floating values. The line that finally worked is let mut num1: f64 = 10e9 + 1.0;
My question is twofold. First, I'm sure there is some good rationale as to why Rust doesn't recognize the caret ^ notation for working with exponents, but it doesn't make a lot of sense to me. Could someone explain why that is so? Thanks.
Second, I also find it odd that Rust won't let me use scientific notation on integers. I also need an explanation for that question, too.
I guess since you have googled extensively you already know that more than half languages out of top20 don't recognize caret for scientific notation and the same goes for using scientific notation with integers.
Yet that doesn't convince you.
I guess the question, then, becomes: why have Rust picked approach used by computers and engineering calculators since before I was born and not something which would be familiar for workers of some other industry.
My guess would be that Rust is not trying to create a revolution and doesn't try to do something strange and unusual. It picks syntax familiar for the field of programming languages not for for the field of math or chemistry.
Does it make any sense to you?
This choice would have, probably, been bad fit for Rust.
I wonder how many non-esoteric programming languages use caret for power. I know about Visual Basic (and some other versions of BASIC, I guess). Anything else? Not even Fortran supports it, it uses ** for power. I guess you can count TeX (although it's not a programming language but it's often used for documents thus people from IT-industry know if), too. Anything else?
I tried to make a macro out of it which doesn't require the type being repeatedly specified for the base 10 (as in 10i32, which feels annoying), but didn't get it to work. How could such a macro look like, which doesn't require the i32 hint?
Thank you, everyone, for your answers. That really helped. Keeping Rust consistent with most of the major programming languages by limiting the caret symbol to bitwise operations was likely a good call. @khimru, you explained that very well.
I see what you are saying about "relative error" and "absolute error", but I'm not convinced that limiting scientific notation to floats only was a good call. Why not both? Of course, the decision was made by wiser heads than mine and I can work around it by adding a decimal point to my integers, but it seems clumsy and unnecessary. Scientific notation is also simply good shorthand for very large numbers. When used that way it's not about error, just utility.
Hmmm..... Come to think of it though, using scientific notation for integers is going to be very limited. As soon as you get past the single digits you have to use a decimal point which forces one into using a float. For instance one could use 9*10e9 to represent to represent 9 billion, but if you want to do 99 billion you need to use a decimal point 9.9*10e9. So, I guess not allowing integers to use scientific notation does make sense. Have I answered my own question?