7 ms·
let x = 5i; so, integers are written like complex numbers?
by gosub 12y ago
let x = 5i;
so, integers are written like complex numbers?
- thristian 12y agoIn this case, "i" is a type suffix meaning "int". 5u would be an unsigned int; 5i32 would be a 32-bit int; 5f64 would be a double precision float, and so forth.
- gosub 12y agoI understood that, but it's an unfortunate suffix.
- pestaa 12y agoI agree. Isn't it the default though? Could be implied when you choose to leave it out.
- hornetblack 12y agoYes it could be implied if left out. Due to type inference. `5` would be signed or unsigned if left off. Depends on how you use it. So you could try. use std::num::abs; let v = vec!['a', 'b', 'c', 'd']; let x = 3; let c = v[x]; // v.index (operator overload) constrains x to type uint. let y = abs(x); // error: failed to find an implementation of trait core::num::Signed for uint. // abs can't constain the type to an int because abs can take anything that // implements `Signed` (eg BigInt) If you wanted to make sure that x was signed. let v = vec!['a', 'b', 'c', 'd']; let x = 3i; let y = abs(x); // Ok as x is signed. let c = v[x]; // error: mismatched types: expected `uint` but found `int` If you really wanted to index to try an index with an int you can use a checked cast. let casted = x.to_uint(); // Returns None if x is negative or Some(x) if it's postive. Also a generic int doesn't have to be int or uint, it can be any integer value: signed: i8, i16, i32, i64 or int. Unsigned: u8, u16, u32, u64 or uint.
- the_mitsuhiko 12y agoComplex numbers use `j` :)
- georgemcbay 12y agoAFAICT they only need to be written like that if you want to assign them to an implicitly typed variable or you can use the alternate explicitly-typed form (which the guide also shows): let x: int = 5; As far as i being an unfortunate suffix, I can't think of a better one when you consider there is already a pattern to such suffixes (being the first letter of the type) carried over from C/C++, et al. I suspect mathematician programmers who are already used to * being multiply and ^ being bitwise xor (instead of, say, an exponent operation) can probably learn to deal with it, or just avoid it by avoiding implicit typing for ints.
- blt 12y ago5s for 5 signed?
- georgemcbay 12y agoThat's an option. I think for me personally the implication that the number is somehow pluralized is more potentially confusing than the idea that it is irrational, but YMMV.
- sixbrx 12y agoOn the other hand, not many languages use lowercase 'i' for complex number literals, in fact I can't think of any right now.
- cheepin 12y agoI can't even think of a language that has first class complex numbers, let alone complex number literals.
- Serow225 12y agoMATLAB
- ori_b 12y agoC? #include <complex.h> complex f(complex a, complex b) { return a*b + 5*I; }
- kragniz 12y agoPython does. >>> 5 + 2j (5+2j) >>> 2j * 2j (-4+0j)
- deleted 12y ago[deleted]
- burntsushi 12y agoGo does too. http://play.golang.org/p/ficmYvVehL http://play.golang.org/p/ficmYvVehL
- gus_massa 12y agoScheme and Racket use i in complex numbers, for example: (* 2+3i 4+5i) ;=> -7+22i
- dragonwriter 12y agoRuby 2.1 does.
- steveklabnik 12y agohttps://github.com/rust-lang/rust/issues/15526 https://github.com/rust-lang/rust/issues/15526 <- there's actually a lot of context for this, and depending on how the RFC shakes out, much of this will go away.