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

85%
+10 −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

There is one thing to clarify when mapping a float interval to an integer interval, namely how the boundaries of the float interval shall be mapped to the boundaries of the integer interval. Takin...

posted 2y ago by Dirk Herrmann‭  ·  edited 2y ago by Dirk Herrmann‭

Answer
#2: Post edited by user avatar Dirk Herrmann‭ · 2022-12-20T22:13:12Z (about 2 years ago)
  • There is one thing to clarify when mapping a float interval to an integer interval, namely how the boundaries of the float interval shall be mapped to the boundaries of the integer interval.
  • Taking the example of the float interval [-1.0, +1.0], assuming here, that the endpoints of the interval can actually occur. If the target integer interval is 0..319: Which floating point interval shall be mapped to 0?
  • If we linearly map [-1.0, +1.0] to [0.0, 319.0] and round to the nearest integer, then this gives (as others have explained) the following formula: y = 159.5x + 159.5.
  • This will have the following effect:
  • * float numbers [-1.0, −0,996865204] will be mapped to 0. This interval has a width of 0,003134796.
  • * float numbers [−0,996865205, −0,990595611] will be mapped to 1. This interval has a width of 0,006269593.
  • * float numbers [0,996865204, +1,0] will be mapped to 319. This interval has a width of 0,003134796.
  • You see that the ranges of float values that map to 0 and 319 are only half as wide as the ranges of float values that map to numbers from 1..318. This may be exactly what you want, or it may be acceptable, in which case the formula y = 159.5x + 159.5 is perfectly fine.
  • If instead you want to achieve that for each target integer number the source intervals shall be equally wide, then you would have to calculate the mapping from [-1.0, +1.0] to [-0.5, 319.5). This would result in the formula y = 160.0*x + 159.5 with rounding, giving the desired numbers from 0..319, plus a special case handling of +1.0 which would have to be mapped to 319 rather than 320.
  • There is one thing to clarify when mapping a float interval to an integer interval, namely how the boundaries of the float interval shall be mapped to the boundaries of the integer interval.
  • Taking the example of the float interval [-1.0, +1.0], assuming here, that the endpoints of the interval can actually occur. If the target integer interval is 0..319: Which floating point interval shall be mapped to 0?
  • If we linearly map [-1.0, +1.0] to [0.0, 319.0] then this gives (as others have explained) the following formula: y = 159.5x + 159.5.
  • When rounding is done to the nearest integer this will have the following effect:
  • * float numbers [-1.0, −0,996865204] will be mapped to 0. This interval has a width of 0,003134796.
  • * float numbers [−0,996865205, −0,990595611] will be mapped to 1. This interval has a width of 0,006269593.
  • * float numbers [0,996865204, +1,0] will be mapped to 319. This interval has a width of 0,003134796.
  • You see that the ranges of float values that map to 0 and 319 are only half as wide as the ranges of float values that map to numbers from 1..318. This may be exactly what you want, or it may be acceptable, in which case the formula y = 159.5x + 159.5 is perfectly fine.
  • For other rounding strategies (round towards minus infinity, round towards plus infinity) the results will be different, but there will still be an asymmetry with respect to the width of the intervals for some the numbers.
  • If instead you want to achieve that for each target integer number the source intervals shall be equally wide, then you would have to calculate the mapping from [-1.0, +1.0] to [-0.5, 319.5). This would result in the formula y = 160.0*x + 159.5 with rounding, giving the desired numbers from 0..319, plus a special case handling of +1.0 which would have to be mapped to 319 rather than 320.
#1: Initial revision by user avatar Dirk Herrmann‭ · 2022-12-20T22:08:30Z (about 2 years ago)
There is one thing to clarify when mapping a float interval to an integer interval, namely how the boundaries of the float interval shall be mapped to the boundaries of the integer interval.

Taking the example of the float interval [-1.0, +1.0], assuming here, that the endpoints of the interval can actually occur.  If the target integer interval is 0..319: Which floating point interval shall be mapped to 0?

If we linearly map [-1.0, +1.0] to [0.0, 319.0] and round to the nearest integer, then this gives (as others have explained) the following formula: y = 159.5x + 159.5.

This will have the following effect:

* float numbers [-1.0, −0,996865204] will be mapped to 0.  This interval has a width of 0,003134796.
* float numbers [−0,996865205, −0,990595611] will be mapped to 1.  This interval has a width of 0,006269593.
* float numbers [0,996865204, +1,0] will be mapped to 319.  This interval has a width of 0,003134796.

You see that the ranges of float values that map to 0 and 319 are only half as wide as the ranges of float values that map to numbers from 1..318.  This may be exactly what you want, or it may be acceptable, in which case the formula y = 159.5x + 159.5 is perfectly fine.

If instead you want to achieve that for each target integer number the source intervals shall be equally wide, then you would have to calculate the mapping from [-1.0, +1.0] to [-0.5, 319.5).  This would result in the formula y = 160.0*x + 159.5 with rounding, giving the desired numbers from 0..319, plus a special case handling of +1.0 which would have to be mapped to 319 rather than 320.