These are chat archives for rust-lang/rust

10th
May 2017
Sathya Narrayanan
@sourcepirate
May 10 2017 09:36
fn fact(n: usize) -> usize{
  match n {
    0 | 1 => 1,
    x => x * fact(x-1)
  }
}

fn fact_loop(n: usize) -> usize{
  (1..n+1).fold(1, |acc , i| {acc*i})
}

fn main(){
  println!("{:?}", fact(120));
  println!("{:?}", fact_loop(120));
}
I have been trying to run the above code but i am getting a "arithmetic operation overflowed" . I don't know why that is happening.
i tried replacing usize with u64 as well no effect at all
Denis Lisov
@tanriol
May 10 2017 09:38
Sorry, the factorial of 120 is too big for even a 64-bit type
Sathya Narrayanan
@sourcepirate
May 10 2017 09:38
is there any alternative way to solve it..
I am trying to use rust on statistics.
where factorial of 120 is too small compared to other numbers
Denis Lisov
@tanriol
May 10 2017 09:39
I'd just use f64. Any reasons not to?
Sherzod Mutalov
@shmutalov
May 10 2017 09:40
Maybe I am wrong, but f64 is small to for 120! )
Sergey Noskov
@Albibek
May 10 2017 09:41
BigInt maybe?
Denis Lisov
@tanriol
May 10 2017 09:42
Ouch. Do you need to actually store the number? Maybe store just its logarithm?
@shmutalov Checked, not yet... the overflow comes for f64 at around 170!
Sathya Narrayanan
@sourcepirate
May 10 2017 09:44
i am currently storing it as logrithm. but i need to calculate factorial till 250 numbers
do have to go for gamma function way to do this ??
Daniel Collin
@emoon
May 10 2017 09:47
I wonder if anyone knows how to solve this https://is.gd/Lafnuf - It's a very simple macro that should support matching foo: and bar: (and more values) each in the format of name: something, If I remove either foo or bar in the macro (and in the macro invokation) it works fine but not with both.
Denis Lisov
@tanriol
May 10 2017 09:53
@sourcepirate What precision do you need?
Sathya Narrayanan
@sourcepirate
May 10 2017 09:55
4 digits after point
:)
the reason i asked is i am trying to do binomial distrbution function. Which needs (n choose k)
so the result will be
(fact(n) / (fact(n-k)*fact(k))) * p.powi(k) * (1-p).powi(n-k)
Ewan Higgs
@ehiggs
May 10 2017 10:15
@emoon even better: https://is.gd/FfM9z5
Daniel Collin
@emoon
May 10 2017 10:15
@ehiggs thanks! :)
Ewan Higgs
@ehiggs
May 10 2017 10:15
based on examples from here: v
@emoon, I think each 'match' needs to be it's own macro. And if you need to repeat a match, you need to split it into a 'foreach' macro and a match macro
Daniel Collin
@emoon
May 10 2017 10:18
I see. The fact that you need to split it like this wasn't (at least to me) directly obvious
Ewan Higgs
@ehiggs
May 10 2017 10:21
i agree. I think I only thought of this because of poking through cargo's code and saw something similarish
you might be able to do it by recursion like the write_html example here: https://doc.rust-lang.org/book/macros.html
but then you have recursion.
Daniel Collin
@emoon
May 10 2017 10:22
Yeah I thought about that but I figured it should be possible without it.
without recursion that is.
If you have a more tree like structure it would make sense to use recursion for sure but in this case it's a bit simpler
Sathya Narrayanan
@sourcepirate
May 10 2017 10:38
Hey thanks @red75prime @ehiggs
Ravi Teja
@tekjar
May 10 2017 10:48
Hi. Has anyone here tried building a crate inside android's build system?
Daniel Collin
@emoon
May 10 2017 11:05
Nope, I "ported" Cargo to run inside another buildsystem (Tundra) and there were a bunch of gotchas along the way for sure. It also really dependens on how complex you need it to be. In my case I needed it to depend on other native (C/C++ libs) built from the outside which made it a bit more complicated
Sathya Narrayanan
@sourcepirate
May 10 2017 11:18
@red75prime the solution you provided will work on rustc >= 1.15. Its an unstable feature in rustc <= 1.9
its called iter_arith
Denis Lisov
@tanriol
May 10 2017 11:21
@sourcepirate Do you mean the sum method? Well, you always can rewrite that line with a loop, can't you?
Sathya Narrayanan
@sourcepirate
May 10 2017 11:22
yes . But it works with fold though
Denis Lisov
@tanriol
May 10 2017 11:23
Do you need to support old Rust verisons?
Sathya Narrayanan
@sourcepirate
May 10 2017 11:24
some cases crates may run on our old machine. with older rust version "just to be on safer side"
Ravi Teja
@tekjar
May 10 2017 12:13
@emoon In my case I just need to depend on a library internal to build system
Daniel Collin
@emoon
May 10 2017 12:14
What do you mean internal to the build system? Is it generated as part of the build?
Alexander Larin
@scalar438
May 10 2017 12:16
Why the compiler does not output warnings about unused traits?
Ewan Higgs
@ehiggs
May 10 2017 12:56
@scalar438 sounds like a bug. Do you have a test case?
rust-lang/rust#33091
Reporting unused traits is something that has PRs in the past so if you have a case where you think you think it should be reported, pls report it.
Ewan Higgs
@ehiggs
May 10 2017 13:21
rust-lang/rust#41883
Thomas Schaller
@torkleyy
May 10 2017 18:26
Hey guys, stupid question: After making changes to a path dependency of my crate, should I immediately publish the new version of that dependency on crates.io or is it sufficient to do that before I publish the new version of my dependent crate?
Ashley Mannix
@KodrAus
May 10 2017 21:29
@torkleyy That doesn't sound like a stupid question at all. On the contrary it's an excellent question. And my answer is it depends :) These days I tend to let the code simmer for a bit before publishing a new version in case I notice something dumb I've done. If that crate is only being used by you then publishing a new version as late as possible (just before the dependent) sounds reasonable. If it's not just you though then once you're happy with the state of it then publishing as soon as possible sounds reasonable too
Am I answering your question or have I misunderstood it?