Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

77%
+5 −0
Q&A How to proportionally convert a number in the range of -1 and 1 to a number in the range of 0 and 319

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...

posted 2y ago by Canina‭  ·  edited 2y ago by Canina‭

Answer
#2: Post edited by user avatar Canina‭ · 2022-12-13T19:52:01Z (about 2 years ago)
  • 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 by user avatar Canina‭ · 2022-12-13T19:47:07Z (about 2 years ago)
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.