Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify CoordinateNumber.gerber() #9

Merged
merged 3 commits into from Nov 21, 2016

Conversation

ubruhin
Copy link
Contributor

@ubruhin ubruhin commented Nov 20, 2016

I think this makes the function CoordinateNumber.gerber() more robust and easier to understand.

// Format invariants
if format.decimal > DECIMAL_PLACES_CHARS {
return Err(GerberError::CoordinateFormatError("Invalid precision: Too high!".into()))
fn rounded_div(dividend: i64, divisor: i64) -> i64 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we should move this method to another location... It would be great to have this functionality in the Rust standard library :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could ask in IRC or on Reddit whether something like that exists in the stdlib, or could possibly be added.

For now, you could create a utils module:

  • Add a utils.rs file and put the function in there (with the pub modifier)
  • Add a mod utils statement to src/lib.rs
  • Add use utils::rounded_div to the top of this file

Copy link
Owner

@dbrgn dbrgn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! :) Not sure what I was thinking when writing the implementation.

// Format invariants
if format.decimal > DECIMAL_PLACES_CHARS {
return Err(GerberError::CoordinateFormatError("Invalid precision: Too high!".into()))
fn rounded_div(dividend: i64, divisor: i64) -> i64 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could ask in IRC or on Reddit whether something like that exists in the stdlib, or could possibly be added.

For now, you could create a utils module:

  • Add a utils.rs file and put the function in there (with the pub modifier)
  • Add a mod utils statement to src/lib.rs
  • Add use utils::rounded_div to the top of this file


// Convert to string
Ok(format!("{}{:0<width$}", integer, decimal, width=format.decimal as usize))
let divisor: i64 = 10_i64.pow((DECIMAL_PLACES_CHARS - format.decimal) as u32);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not actually sure if we need the i64 suffix at all. Could you try without it (also on line 103)? Maybe it can be derived.

let integer: i64 = self.nano / DECIMAL_PLACES_FACTOR;
if integer > 10i64.pow(format.integer as u32) {
return Err(GerberError::CoordinateFormatError("Decimal is too large for chosen format".into()));
if self.nano >= 10_i64.pow((format.integer + DECIMAL_PLACES_CHARS) as u32) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Negatives aren't supported here, right? We probably need self.nano.abs()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! :) Fixed it and also added a test which covers this issue...

Also added a test which covers this issue.
let divisor: i64 = 10_i64.pow((DECIMAL_PLACES_CHARS - format.decimal) as u32);
let number: i64 = rounded_div(self.nano, divisor);
let divisor = 10_i64.pow((DECIMAL_PLACES_CHARS - format.decimal) as u32);
let number = rounded_div(self.nano, divisor);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I meant it the other way around. Doesn't this work?

let divisor: i64 = 10.pow((DECIMAL_PLACES_CHARS - format.decimal) as u32);
let number: i64 = rounded_div(self.nano, divisor);

Then it's still clear and explicit what the types are, but unnecessary literal suffixes aren't there. (But again, I'm not 100% sure if the type inference works properly here.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried it, but it didn't work :(

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, then I'd simply undo commit 025494a (git reset --hard HEAD^ && git push --force).

@ubruhin
Copy link
Contributor Author

ubruhin commented Nov 21, 2016

Now it's perfect :)

@dbrgn dbrgn merged commit 9bc4675 into dbrgn:master Nov 21, 2016
@dbrgn
Copy link
Owner

dbrgn commented Nov 21, 2016

Awesome! Thanks @ubruhin and @Luz.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants