Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
My initial approach would be to increase n by 1 (thus shifting the original range from -1..+1 to 0..+2), turn that range into 0..+1, and then simply map from that to your 0..+319 output range. flo...
Answer
#2: Post edited
- My initial approach would be to increase `n` by 1 (thus shifting the original range from -1..+1 to 0..+2), turn that range into 0..+1, and then simply map from that to your 0..+319 output range.
- float result = (n+1)/2 * 319;
- This will map -1 to 0; 0 to 159.5; +1 to 319; and your example +0.12345 to 179.190275. (All potentially subject to float precision issues, of course.)
- As for *efficiency*, this is a few simple math operations; I'm not sure you can get much more efficient than that. That problem is complicated by the fact that you are dealing with a floating-point value as the input.
- Had the input value `n` been an integer, you could probably just have precomputed a fairly small (320 entries in this case) table and benchmarked on-need calculation and table lookup respectively on application startup and used the faster approach, paying attention to things like cache locality, but this feels as though if that's the level of optimization you are looking for, then Java is the wrong language to use to begin with.
- My initial approach would be to increase `n` by 1 (thus shifting the original range from -1..+1 to 0..+2), turn that range into 0..+1, and then simply map from that to your 0..+319 output range.
- float result = (n+1)/2 * 319;
- This will map -1 to 0; 0 to 159.5; +1 to 319; and your example +0.12345 to 179.190275. (All potentially subject to float precision issues, of course.)
- As for *efficiency*, this is a few simple math operations; I'm not sure you can get much more efficient than that. That problem is complicated by the fact that you are dealing with a floating-point value as the input.
- Once you have the corresponding floating-point value, just use whatever rounding function you prefer. java.lang.Math offers e.g. `floor()` and `rint()`, or if you think you can do better then you can roll your own rounding function.
- Had the input value `n` been an integer, you could probably just have precomputed a fairly small (320 entries in this case) table and benchmarked on-need calculation and table lookup respectively on application startup and used the faster approach, paying attention to things like cache locality, but this feels as though if that's the level of optimization you are looking for, then Java is the wrong language to use to begin with.
#1: Initial revision
My initial approach would be to increase `n` by 1 (thus shifting the original range from -1..+1 to 0..+2), turn that range into 0..+1, and then simply map from that to your 0..+319 output range. float result = (n+1)/2 * 319; This will map -1 to 0; 0 to 159.5; +1 to 319; and your example +0.12345 to 179.190275. (All potentially subject to float precision issues, of course.) As for *efficiency*, this is a few simple math operations; I'm not sure you can get much more efficient than that. That problem is complicated by the fact that you are dealing with a floating-point value as the input. Had the input value `n` been an integer, you could probably just have precomputed a fairly small (320 entries in this case) table and benchmarked on-need calculation and table lookup respectively on application startup and used the faster approach, paying attention to things like cache locality, but this feels as though if that's the level of optimization you are looking for, then Java is the wrong language to use to begin with.