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

floats and range #2

Open
data-man opened this issue Nov 12, 2020 · 10 comments
Open

floats and range #2

data-man opened this issue Nov 12, 2020 · 10 comments

Comments

@data-man
Copy link

const std = @import("std");
const it = @import("ziter.zig");

fn add(a: f32, b: f32) f32 { return a + b; }

test "f32, 0, 1, 0.1" {
	const result = it.range_ex(f32, 0, 1, 0.1)
	.call(it.fold, .{@as(f32, 0), add});
	std.debug.print("result = {}\n", .{result});
}

test "f32, 0, 1, 0.3" {
	const result = it.range_ex(f32, 0, 1, 0.3)
	.call(it.fold, .{@as(f32, 0), add});
	std.debug.print("result = {}\n", .{result});
}

test "f32, 0, 1, 0.1"... result = 4.5e+00 //correct
test "f32, 0, 1, 0.3"... result = 1.80000007e+00 //incorrect

@Hejsil
Copy link
Owner

Hejsil commented Nov 17, 2020

Ops, looked at this but forgot to respond. Isn't this just quirk of floating point numbers. Not really much this lib can do about that.

@data-man
Copy link
Author

Is it possible to: not provide len_hint if a given type is some float?

And the related proposal: make range inclusive, like C++ std::iota.

@Hejsil
Copy link
Owner

Hejsil commented Nov 17, 2020

Is it possible to: not provide len_hint if a given type is some float?

Not sure what you mean. Do you mean that range should not provide len_hint for floats?

And the related proposal: make range inclusive, like C++ std::iota.

I made it exclusive as it follows how slicing works in zig, so zig programmers are familiar with it.

@data-man
Copy link
Author

Do you mean that range should not provide len_hint for floats?

Yes.

I made it exclusive as it follows how slicing works in zig

Ok. What about new pub fn range_i? (or similar name)

@Hejsil
Copy link
Owner

Hejsil commented Nov 17, 2020

I see. Yea, both of these sounds good to be 👍

@data-man
Copy link
Author

Or to add new parameter to range_ex: comptime inclusive: bool?

@Hejsil
Copy link
Owner

Hejsil commented Nov 17, 2020

Hmmmmm. How about:

pub fn range(comptime T: type, start: T, end: T, opts: struct { inclusive: bool = false, step: T = 1 }) Range(T) {
    return .{ .start = start, .end = end + (opt.step * @boolToInt(opt.inclusive)), .step = opt.step };
}
...

range(u8, 0, 6, .{});
range(u8, 0, 6, .{ .step = 2 });

Basically, having and options argument with all fields having default values.

@data-man
Copy link
Author

How about:

Looks good! 👍

range(u8, 0, 6, .{ .step = 2 });

What will happen for range(u8, 0, 255, .{ .step = 2 });?

@Hejsil
Copy link
Owner

Hejsil commented Nov 17, 2020

What will happen for range(u8, 0, 255, .{ .step = 2 });?

Well, that would overflow with the current behavior. We could have an implementation that handles this, which might be nice, but it probably has a cost.

@Hejsil
Copy link
Owner

Hejsil commented Feb 16, 2023

As of 0c2a668, ziter.range no longer supports floats and step has been removed in favor of:

ziter.range(u8, 0, 10)
    .step_by(2)

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

No branches or pull requests

2 participants