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?
Rust have many innovations for IT field (actually they are, mostly, things that are 20+ years old), but as far as syntax is concerned it tries to follow most popular curly-bracket languages: C, C++, Java, JavaScript, PHP…
JavaScript, of course, allows you to use e-notation with arrays and string indexes but that's not because it allows you to use it with integers but because it doesn't have integers at all!
This choice would have, probably, been bad fit for Rust.
Because scientific notation is all about relative error, but integers are about absolute error. Generally, if you want scientific notation, I'd say you frequently want floating-point.
If you want 1 GiB, you can use "binary scientific notation": 1 << 30. If you want 1 MB, remember you can use separators: 1_000_000.
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.
I agree it would be nice. Something like this could even probably be added in a backwards compatible way, for instance: 1.99e9_u32.
I don't think the "relative error" counter-argument is convincing. In science this might be sometimes taken to mean something like (1.99 ± 0.01) * 109, but even for f64 it's not what it means.
1.123e2_u32 would be disallowed, but 1.99e9_u32 would work.
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?
You could write 99 billion either as 99e9_u64 or 9.9e10_u64. I don't see why would want to limit the notation to single digits or disallow the decimal point.