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 »
Sandbox

Post History

71%
+3 −0
#7: Post edited by user avatar trichoplax‭ · 2023-06-19T10:25:14Z (over 1 year ago)
Add finalized tag now that the sandbox can be filtered to exclude tags
Encode and decode floating point integers [FINALIZED]
Imagine you have only one byte (8 bits) to store a value, but need to store values from $0$ to $4032$. Impossible, until you are also told that an error of 1/64 of the exact value does not matter.

With that knowledge, you decide to design a floating point integer type, which is defined as follows:

  * All representable numbers are of the form $m\cdot 2^e$, where $m$ is the mantissa and $e$ is the exponent. There are no NaNs and no infinities.

  * The upper three bits of the byte are the exponent, the lower five are the mantissa. Since no negative numbers are stored, no sign bit is needed.

    I'll refer to the three bit number represented by the exponent bits as $E$, and the 5 bit value represented by the mantissa bits as $M$

  * If $E=0$, then $e=0$ and $m=M$ (“denormalized integer”).

  * Otherwise, $e=E-1$ and $m=M+32$

Your task is now to write code that converts a positive integer to that floating point format and back. In particular:

  * Encoding:

      - Your code takes an integer $0\le n\le 4032$, and outputs
        an integer $0\le r\le 255$. You can assume that the input
        integer is always in the allowed range (that is, no overflow
        handling is required)

      - If $n$ can be represented exactly, $r$ is the
        corresponding representation.

      - Otherwise, if $n$ is exactly in the middle of two
        consecutive exactly representable numbers, $r$ is the
        choice with the even mantissa.

      - Otherwise, $r$ is the representation of the closest
        exactly representable number.

  * Decoding:

    Your code takes an integer $0\le r\le 255$ and outputs
    the integer $0\le n\le 4032$ it represents.

You can write either two separate routines for encoding and decoding, or a single routine that takes an extra argument telling whether to encode or decode. Here “routine” refers to any form of executable code taking input and giving output (program, function, macro, …).

This is <span class="badge is-tag">code-golf</span>, that is the shortest code wins. If you choose separate encoding and decoding routines, the score is the sum of the sizes of the encoding and decoding routines.

An ungolfed (and unoptimized) Python implementation is available [here][Python implementation]

Test cases:

Encoding:

     input  output
        0      0
        1      1
       31     31
       32     32
       63     63
       64     64
       65     64
       66     65
       67     66
       68     66
       69     66
       70     67
      123     94
      124     94
      125     94
      126     95
      127     96
      128     96
      129     96
      130     96
      131     97
      256    128
     2021    223
     4032    255

Decoding:

    input  output
       0     0 
       1     1
      31    31
      32    32
      63    63
      64    64
      65    66
      66    68
      93   122
      94   124
      95   126
      96   128
      97   132
     128   256
     160   512
     223  2016
     224  2048
     255  4032


[Python implementation]: https://tio.run/##lVZdb9owFH3Pr7jqtCmhVAohoQ0qSGxjElrTTYOHalWFUmJGpuBEibNuYv3t7NqOIQGDtAiZ2Of6fp1jQ/aHrVLa3W6D0f1sMp2O5u8nsykMwDN2K8Fo@hlXHGi1oGl2BR0jmNzP7798C0Z3k@/jj1o7dPUwHz@MPsw43GruMAwjIksgdJFGxKR9iCmz4GrIv/sG4BMvgcItNLdJiD85YWVOgRpi5Q1EhJF8HVMCbEWA/M5SSigTIMH4tnh7WcUJQbdDnlCLAM@4ynHvmcDlACsU82W4YGkO0lyFKrIkZkDL9TPJeb4psLyki5CRCNYhZXFRhBDSCJK0YPAcs0Js5LM5n6E7Cu/ArJxjOy1hEMz3fgZgYppDIBZaNjhRWYTRzxLdVwEJLNFVlqcZ5pSnJY1i@kP10WntYw9VTfgx68BgoBCeej2Xt0guoh1r36MAE6ybXGLDRO@Sgpy0UpnPkKBd9xp0iWZBXADOYJ3mnMxQmmBqZZg0iR2jfyJiV56xVlU7CqQoE54bxtixkv4i@TJJX9qqfTnJ0BAdhixOqWpYwOs9pbyx1Ee9SFsmUGnSHMPtbfMsWPAXgkrzERGazzWa5wXlnPbmQVJhcr0WMGEMiRnbVl3FSvT8WXMmNAxxq7E40A1L7OnRca0VuMaDIw7EdonU8sZxTW@EyYU80hf96my35aosGlfli1h8rRrCSMFM5WlOwzXpQ8HyNvYlK1n1npZMTaoqJcEYeJfEY8PJkyn2W7smqQ0D5WxXdJYjBeby4utoOkWqYdNw9GpuZCZD99XihUpHfHphaZqqvH0aTe7@y1sbilVaJhE8E9hUOcoghsGbtAgLwjv9KEKZqtVtPrPlaLU1WEeOOqzbkaMWc@Sow3pdOWoxV45azDuD9aSFFruWFlrs5gzmn8aubelZg3UcUZ/vajH3DOadwUR9vqfFRH1@T4vdnMH801jXPoMJ3n1d7Y7Xk1GPMcd2@D7H0fDu2kIvjsfrq8Dq1GsFWscOBVrDjgRaxw4FWsOOBFrHDgVax7wDwdQxKdAbHeZ3RdO0ufiulI0W86Q0tNghEXXsWtCoiycFgzTqsB7nwevo9jlc9I7d6Wkxl2OuLhekXNIvL8Mnw@D/RdSFJX7j1eUlL0lx4bfUomVs/wE
#6: Post edited by user avatar celtschk‭ · 2021-10-01T16:51:52Z (about 3 years ago)
  • Encode and decode floating point integers
  • Encode and decode floating point integers [FINALIZED]
#5: Post edited by user avatar celtschk‭ · 2021-09-25T11:35:29Z (over 3 years ago)
  • Imagine you have only one byte (8 bits) to store a value, but need to store values from $0$ to $4032$. Impossible, until you are also told that an error of 1/64 of the exact value does not matter.
  • With that knowledge, you decide to design a floating point integer type, which is defined as follows:
  • * All representable numbers are of the form $m\cdot 2^e$, where $m$ is the mantissa and $e$ is the exponent. There are no NaNs and no infinities.
  • * The upper three bits of the byte are the exponent, the lower five are the mantissa. Since no negative numbers are stored, no sign bit is needed.
  • I'll refer to the three bit number represented by the exponent bits as $E$, and the 5 bit value represented by the mantissa bits as $M$
  • * If $E=0$, then $e=0$ and $m=M$ (“denormalized integer”).
  • * Otherwise, $e=E-1$ and $m=M+32$
  • Your task is now to write code that converts a positive integer to that floating point format and back. In particular:
  • * Encoding:
  • - Your code takes an integer $0\le n\le 4032$, and outputs
  • an integer $0\le r\le 255$. You can assume that the input
  • integer is always in the allowed range (that is, no overflow
  • handling is required)
  • - If $n$ can be represented exactly, $r$ is the
  • corresponding representation.
  • - Otherwise, if $n$ is exactly in the middle of two
  • consecutive exactly representable numbers, $r$ is the
  • choice with the even mantissa.
  • - Otherwise, $r$ is the representation of the closest
  • exactly representable number.
  • * Decoding:
  • Your code takes an integer $0\le r\le 255$ and outputs
  • the integer $0\le n\le 4032$ it represents.
  • You can write either two separate routines for encoding and decoding, or a single routine that takes an extra argument telling whether to encode or decode. Here “routine” refers to any form of executable code taking input and giving output (program, function, macro, …).
  • This is code golf, the shortest code wins. If you choose separate encoding and decoding routines, the score is the sum of the sizes of the encoding and decoding routines.
  • An ungolfed (and unoptimized) Python implementation is available [here][Python implementation]
  • Test cases:
  • Encoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 64
  • 66 65
  • 67 66
  • 68 66
  • 69 66
  • 70 67
  • 123 94
  • 124 94
  • 125 94
  • 126 95
  • 127 96
  • 128 96
  • 129 96
  • 130 96
  • 131 97
  • 256 128
  • 2021 223
  • 4032 255
  • Decoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 66
  • 66 68
  • 93 122
  • 94 124
  • 95 126
  • 96 128
  • 97 132
  • 128 256
  • 160 512
  • 223 2016
  • 224 2048
  • 255 4032
  • [Python implementation]: https://tio.run/##lVZdb9owFH3Pr7jqtCmhVAohoQ0qSGxjElrTTYOHalWFUmJGpuBEibNuYv3t7NqOIQGDtAiZ2Of6fp1jQ/aHrVLa3W6D0f1sMp2O5u8nsykMwDN2K8Fo@hlXHGi1oGl2BR0jmNzP7798C0Z3k@/jj1o7dPUwHz@MPsw43GruMAwjIksgdJFGxKR9iCmz4GrIv/sG4BMvgcItNLdJiD85YWVOgRpi5Q1EhJF8HVMCbEWA/M5SSigTIMH4tnh7WcUJQbdDnlCLAM@4ynHvmcDlACsU82W4YGkO0lyFKrIkZkDL9TPJeb4psLyki5CRCNYhZXFRhBDSCJK0YPAcs0Js5LM5n6E7Cu/ArJxjOy1hEMz3fgZgYppDIBZaNjhRWYTRzxLdVwEJLNFVlqcZ5pSnJY1i@kP10WntYw9VTfgx68BgoBCeej2Xt0guoh1r36MAE6ybXGLDRO@Sgpy0UpnPkKBd9xp0iWZBXADOYJ3mnMxQmmBqZZg0iR2jfyJiV56xVlU7CqQoE54bxtixkv4i@TJJX9qqfTnJ0BAdhixOqWpYwOs9pbyx1Ee9SFsmUGnSHMPtbfMsWPAXgkrzERGazzWa5wXlnPbmQVJhcr0WMGEMiRnbVl3FSvT8WXMmNAxxq7E40A1L7OnRca0VuMaDIw7EdonU8sZxTW@EyYU80hf96my35aosGlfli1h8rRrCSMFM5WlOwzXpQ8HyNvYlK1n1npZMTaoqJcEYeJfEY8PJkyn2W7smqQ0D5WxXdJYjBeby4utoOkWqYdNw9GpuZCZD99XihUpHfHphaZqqvH0aTe7@y1sbilVaJhE8E9hUOcoghsGbtAgLwjv9KEKZqtVtPrPlaLU1WEeOOqzbkaMWc@Sow3pdOWoxV45azDuD9aSFFruWFlrs5gzmn8aubelZg3UcUZ/vajH3DOadwUR9vqfFRH1@T4vdnMH801jXPoMJ3n1d7Y7Xk1GPMcd2@D7H0fDu2kIvjsfrq8Dq1GsFWscOBVrDjgRaxw4FWsOOBFrHDgVax7wDwdQxKdAbHeZ3RdO0ufiulI0W86Q0tNghEXXsWtCoiycFgzTqsB7nwevo9jlc9I7d6Wkxl2OuLhekXNIvL8Mnw@D/RdSFJX7j1eUlL0lx4bfUomVs/wE
  • Imagine you have only one byte (8 bits) to store a value, but need to store values from $0$ to $4032$. Impossible, until you are also told that an error of 1/64 of the exact value does not matter.
  • With that knowledge, you decide to design a floating point integer type, which is defined as follows:
  • * All representable numbers are of the form $m\cdot 2^e$, where $m$ is the mantissa and $e$ is the exponent. There are no NaNs and no infinities.
  • * The upper three bits of the byte are the exponent, the lower five are the mantissa. Since no negative numbers are stored, no sign bit is needed.
  • I'll refer to the three bit number represented by the exponent bits as $E$, and the 5 bit value represented by the mantissa bits as $M$
  • * If $E=0$, then $e=0$ and $m=M$ (“denormalized integer”).
  • * Otherwise, $e=E-1$ and $m=M+32$
  • Your task is now to write code that converts a positive integer to that floating point format and back. In particular:
  • * Encoding:
  • - Your code takes an integer $0\le n\le 4032$, and outputs
  • an integer $0\le r\le 255$. You can assume that the input
  • integer is always in the allowed range (that is, no overflow
  • handling is required)
  • - If $n$ can be represented exactly, $r$ is the
  • corresponding representation.
  • - Otherwise, if $n$ is exactly in the middle of two
  • consecutive exactly representable numbers, $r$ is the
  • choice with the even mantissa.
  • - Otherwise, $r$ is the representation of the closest
  • exactly representable number.
  • * Decoding:
  • Your code takes an integer $0\le r\le 255$ and outputs
  • the integer $0\le n\le 4032$ it represents.
  • You can write either two separate routines for encoding and decoding, or a single routine that takes an extra argument telling whether to encode or decode. Here “routine” refers to any form of executable code taking input and giving output (program, function, macro, …).
  • This is <span class="badge is-tag">code-golf</span>, that is the shortest code wins. If you choose separate encoding and decoding routines, the score is the sum of the sizes of the encoding and decoding routines.
  • An ungolfed (and unoptimized) Python implementation is available [here][Python implementation]
  • Test cases:
  • Encoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 64
  • 66 65
  • 67 66
  • 68 66
  • 69 66
  • 70 67
  • 123 94
  • 124 94
  • 125 94
  • 126 95
  • 127 96
  • 128 96
  • 129 96
  • 130 96
  • 131 97
  • 256 128
  • 2021 223
  • 4032 255
  • Decoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 66
  • 66 68
  • 93 122
  • 94 124
  • 95 126
  • 96 128
  • 97 132
  • 128 256
  • 160 512
  • 223 2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HDgVax7wDwdQxKdAbHeZ3RdO0ufiulI0W86Q0tNghEXXsWtCoiycFgzTqsB7nwevo9jlc9I7d6Wkxl2OuLhekXNIvL8Mnw@D/RdSFJX7j1eUlL0lx4bfUomVs/wE
#4: Post edited by user avatar celtschk‭ · 2021-09-25T10:13:01Z (over 3 years ago)
  • Imagine you have only one byte (8 bits) to store a value, but need to store values from $0$ to $4032$. Impossible, until you are also told that an error of 1/64 of the exact value does not matter.
  • With that knowledge, you decide to design a floating point integer type, which is defined as follows:
  • * All representable numbers are of the form $m\cdot 2^e$, where $m$ is the mantissa and $e$ is the exponent. There are no NaNs and no infinities.
  • * The upper three bits of the byte are the exponent, the lower five are the mantissa. Since no negative numbers are stored, no sign bit is needed.
  • I'll refer to the three bit number represented by the exponent bits as $E$, and the 5 bit value represented by the mantissa bits as $M$
  • * If $E=0$, then $e=0$ and $m=M$ (“denormalized integer”).
  • * Otherwise, $e=E-1$ and $m=M+32$
  • Your task is now to write code that converts a positive integer to that floating point format and back. In particular:
  • * Encoding:
  • - Your code takes an integer $0\le n\le 4032$, and outputs
  • an integer $0\le r\le 255$. You can assume that the input
  • integer is always in the allowed range (that is, no overflow
  • handling is required)
  • - If $n$ can be represented exactly, $r$ is the
  • corresponding representation.
  • - Otherwise, if $n$ is exactly in the middle of two
  • consecutive exactly representable numbers, $r$ is the
  • choice with the even mantissa.
  • - Otherwise, $r$ is the representation of the closest
  • exactly representable number.
  • * Decoding:
  • Your code takes an integer $0\le r\le 255$ and outputs
  • the integer $0\le n\le 4032$ it represents.
  • You can write either two separate routines for encoding and decoding, or a single routine that takes an extra argument telling whether to encode or decode. Here “routine” refers to any form of executable code taking input and giving output (program, function, macro, …).
  • This is code golf, the shortest code wins. If you choose separate encoding and decoding routines, the score is the sum of the sizes of the encoding and decoding routines.
  • An ungolfed (an unoptimized) Python implementation is available [here][Python implementation]
  • Test cases:
  • Encoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 64
  • 66 65
  • 67 66
  • 68 66
  • 69 66
  • 70 67
  • 123 94
  • 124 94
  • 125 94
  • 126 95
  • 127 96
  • 128 96
  • 129 96
  • 130 96
  • 131 97
  • 256 128
  • 2021 223
  • 4032 255
  • Decoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 66
  • 66 68
  • 93 122
  • 94 124
  • 95 126
  • 96 128
  • 97 132
  • 128 256
  • 160 512
  • 223 2016
  • 224 2048
  • 255 4032
  • [Python implementation]: https://tio.run/##lVZdb9owFH3Pr7jqtCmhVAohoQ0qSGxjElrTTYOHalWFUmJGpuBEibNuYv3t7NqOIQGDtAiZ2Of6fp1jQ/aHrVLa3W6D0f1sMp2O5u8nsykMwDN2K8Fo@hlXHGi1oGl2BR0jmNzP7798C0Z3k@/jj1o7dPUwHz@MPsw43GruMAwjIksgdJFGxKR9iCmz4GrIv/sG4BMvgcItNLdJiD85YWVOgRpi5Q1EhJF8HVMCbEWA/M5SSigTIMH4tnh7WcUJQbdDnlCLAM@4ynHvmcDlACsU82W4YGkO0lyFKrIkZkDL9TPJeb4psLyki5CRCNYhZXFRhBDSCJK0YPAcs0Js5LM5n6E7Cu/ArJxjOy1hEMz3fgZgYppDIBZaNjhRWYTRzxLdVwEJLNFVlqcZ5pSnJY1i@kP10WntYw9VTfgx68BgoBCeej2Xt0guoh1r36MAE6ybXGLDRO@Sgpy0UpnPkKBd9xp0iWZBXADOYJ3mnMxQmmBqZZg0iR2jfyJiV56xVlU7CqQoE54bxtixkv4i@TJJX9qqfTnJ0BAdhixOqWpYwOs9pbyx1Ee9SFsmUGnSHMPtbfMsWPAXgkrzERGazzWa5wXlnPbmQVJhcr0WMGEMiRnbVl3FSvT8WXMmNAxxq7E40A1L7OnRca0VuMaDIw7EdonU8sZxTW@EyYU80hf96my35aosGlfli1h8rRrCSMFM5WlOwzXpQ8HyNvYlK1n1npZMTaoqJcEYeJfEY8PJkyn2W7smqQ0D5WxXdJYjBeby4utoOkWqYdNw9GpuZCZD99XihUpHfHphaZqqvH0aTe7@y1sbilVaJhE8E9hUOcoghsGbtAgLwjv9KEKZqtVtPrPlaLU1WEeOOqzbkaMWc@Sow3pdOWoxV45azDuD9aSFFruWFlrs5gzmn8aubelZg3UcUZ/vajH3DOadwUR9vqfFRH1@T4vdnMH801jXPoMJ3n1d7Y7Xk1GPMcd2@D7H0fDu2kIvjsfrq8Dq1GsFWscOBVrDjgRaxw4FWsOOBFrHDgVax7wDwdQxKdAbHeZ3RdO0ufiulI0W86Q0tNghEXXsWtCoiycFgzTqsB7nwevo9jlc9I7d6Wkxl2OuLhekXNIvL8Mnw@D/RdSFJX7j1eUlL0lx4bfUomVs/wE
  • Imagine you have only one byte (8 bits) to store a value, but need to store values from $0$ to $4032$. Impossible, until you are also told that an error of 1/64 of the exact value does not matter.
  • With that knowledge, you decide to design a floating point integer type, which is defined as follows:
  • * All representable numbers are of the form $m\cdot 2^e$, where $m$ is the mantissa and $e$ is the exponent. There are no NaNs and no infinities.
  • * The upper three bits of the byte are the exponent, the lower five are the mantissa. Since no negative numbers are stored, no sign bit is needed.
  • I'll refer to the three bit number represented by the exponent bits as $E$, and the 5 bit value represented by the mantissa bits as $M$
  • * If $E=0$, then $e=0$ and $m=M$ (“denormalized integer”).
  • * Otherwise, $e=E-1$ and $m=M+32$
  • Your task is now to write code that converts a positive integer to that floating point format and back. In particular:
  • * Encoding:
  • - Your code takes an integer $0\le n\le 4032$, and outputs
  • an integer $0\le r\le 255$. You can assume that the input
  • integer is always in the allowed range (that is, no overflow
  • handling is required)
  • - If $n$ can be represented exactly, $r$ is the
  • corresponding representation.
  • - Otherwise, if $n$ is exactly in the middle of two
  • consecutive exactly representable numbers, $r$ is the
  • choice with the even mantissa.
  • - Otherwise, $r$ is the representation of the closest
  • exactly representable number.
  • * Decoding:
  • Your code takes an integer $0\le r\le 255$ and outputs
  • the integer $0\le n\le 4032$ it represents.
  • You can write either two separate routines for encoding and decoding, or a single routine that takes an extra argument telling whether to encode or decode. Here “routine” refers to any form of executable code taking input and giving output (program, function, macro, …).
  • This is code golf, the shortest code wins. If you choose separate encoding and decoding routines, the score is the sum of the sizes of the encoding and decoding routines.
  • An ungolfed (and unoptimized) Python implementation is available [here][Python implementation]
  • Test cases:
  • Encoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 64
  • 66 65
  • 67 66
  • 68 66
  • 69 66
  • 70 67
  • 123 94
  • 124 94
  • 125 94
  • 126 95
  • 127 96
  • 128 96
  • 129 96
  • 130 96
  • 131 97
  • 256 128
  • 2021 223
  • 4032 255
  • Decoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 66
  • 66 68
  • 93 122
  • 94 124
  • 95 126
  • 96 128
  • 97 132
  • 128 256
  • 160 512
  • 223 2016
  • 224 2048
  • 255 4032
  • [Python implementation]: https://tio.run/##lVZdb9owFH3Pr7jqtCmhVAohoQ0qSGxjElrTTYOHalWFUmJGpuBEibNuYv3t7NqOIQGDtAiZ2Of6fp1jQ/aHrVLa3W6D0f1sMp2O5u8nsykMwDN2K8Fo@hlXHGi1oGl2BR0jmNzP7798C0Z3k@/jj1o7dPUwHz@MPsw43GruMAwjIksgdJFGxKR9iCmz4GrIv/sG4BMvgcItNLdJiD85YWVOgRpi5Q1EhJF8HVMCbEWA/M5SSigTIMH4tnh7WcUJQbdDnlCLAM@4ynHvmcDlACsU82W4YGkO0lyFKrIkZkDL9TPJeb4psLyki5CRCNYhZXFRhBDSCJK0YPAcs0Js5LM5n6E7Cu/ArJxjOy1hEMz3fgZgYppDIBZaNjhRWYTRzxLdVwEJLNFVlqcZ5pSnJY1i@kP10WntYw9VTfgx68BgoBCeej2Xt0guoh1r36MAE6ybXGLDRO@Sgpy0UpnPkKBd9xp0iWZBXADOYJ3mnMxQmmBqZZg0iR2jfyJiV56xVlU7CqQoE54bxtixkv4i@TJJX9qqfTnJ0BAdhixOqWpYwOs9pbyx1Ee9SFsmUGnSHMPtbfMsWPAXgkrzERGazzWa5wXlnPbmQVJhcr0WMGEMiRnbVl3FSvT8WXMmNAxxq7E40A1L7OnRca0VuMaDIw7EdonU8sZxTW@EyYU80hf96my35aosGlfli1h8rRrCSMFM5WlOwzXpQ8HyNvYlK1n1npZMTaoqJcEYeJfEY8PJkyn2W7smqQ0D5WxXdJYjBeby4utoOkWqYdNw9GpuZCZD99XihUpHfHphaZqqvH0aTe7@y1sbilVaJhE8E9hUOcoghsGbtAgLwjv9KEKZqtVtPrPlaLU1WEeOOqzbkaMWc@Sow3pdOWoxV45azDuD9aSFFruWFlrs5gzmn8aubelZg3UcUZ/vajH3DOadwUR9vqfFRH1@T4vdnMH801jXPoMJ3n1d7Y7Xk1GPMcd2@D7H0fDu2kIvjsfrq8Dq1GsFWscOBVrDjgRaxw4FWsOOBFrHDgVax7wDwdQxKdAbHeZ3RdO0ufiulI0W86Q0tNghEXXsWtCoiycFgzTqsB7nwevo9jlc9I7d6Wkxl2OuLhekXNIvL8Mnw@D/RdSFJX7j1eUlL0lx4bfUomVs/wE
#3: Post edited by user avatar celtschk‭ · 2021-09-25T10:11:00Z (over 3 years ago)
  • Imagine you have only one byte (8 bits) to store a value, but need to store values from $0$ to $4032$. Impossible, until you are also told that an error of 1/64 of the exact value does not matter.
  • With that knowledge, you decide to design a floating point integer type, which is defined as follows:
  • * All representable numbers are of the form $m\cdot 2^e$, where $m$ is the mantissa and $e$ is the exponent. There are no NaNs and no infinities.
  • * The upper three bits of the byte are the exponent, the lower five are the mantissa. Since no negative numbers are stored, no sign bit is needed.
  • I'll refer to the three bit number represented by the exponent bits as $E$, and the 5 bit value represented by the mantissa bits as $M$
  • * If $E=0$, then $e=0$ and $m=M$ (“denormalized integer”).
  • * Otherwise, $e=E-1$ and $m=M+32$
  • Your task is now to write code that converts a positive integer to that floating point format and back. In particular:
  • * Encoding:
  • - Your code takes an integer $0\le n\le 4032$, and outputs
  • an integer $0\le r\le 255$. You can assume that the input
  • integer is always in the allowed range (that is, no error
  • handling is required)
  • - If $n$ can be represented exactly, $r$ is the
  • corresponding representation.
  • - Otherwise, if $n$ is exactly in the middle of two
  • consecutive exactly representable numbers, $r$ is the
  • choice with the even mantissa.
  • - Otherwise, $r$ is the representation of the closest
  • exactly representable number.
  • * Decoding:
  • Your code takes an integer $0\le r\le 255$ and outputs
  • the integer $0\le n\le 4032$ it represents.
  • You can write either two separate routines for encoding and decoding, or a single routine that takes an extra argument telling whether to encode or decode. Here “routine” refers to any form of executable code taking input and giving output (program, function, macro, …).
  • This is code golf, the shortest code wins. If you choose separate encoding and decoding routines, the score is the sum of the sizes of the encoding and decoding routines.
  • An ungolfed (an unoptimized) Python implementation is available [here][Python implementation]
  • Test cases:
  • Encoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 64
  • 66 65
  • 67 66
  • 68 66
  • 69 66
  • 70 67
  • 123 94
  • 124 94
  • 125 94
  • 126 95
  • 127 96
  • 128 96
  • 129 96
  • 130 96
  • 131 97
  • 256 128
  • 2021 223
  • 4032 255
  • Decoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 66
  • 66 68
  • 93 122
  • 94 124
  • 95 126
  • 96 128
  • 97 132
  • 128 256
  • 160 512
  • 223 2016
  • 224 2048
  • 255 4032
  • [Python implementation]: https://tio.run/##lVZdb9owFH3Pr7jqtCmhVAohoQ0qSGxjElrTTYOHalWFUmJGpuBEibNuYv3t7NqOIQGDtAiZ2Of6fp1jQ/aHrVLa3W6D0f1sMp2O5u8nsykMwDN2K8Fo@hlXHGi1oGl2BR0jmNzP7798C0Z3k@/jj1o7dPUwHz@MPsw43GruMAwjIksgdJFGxKR9iCmz4GrIv/sG4BMvgcItNLdJiD85YWVOgRpi5Q1EhJF8HVMCbEWA/M5SSigTIMH4tnh7WcUJQbdDnlCLAM@4ynHvmcDlACsU82W4YGkO0lyFKrIkZkDL9TPJeb4psLyki5CRCNYhZXFRhBDSCJK0YPAcs0Js5LM5n6E7Cu/ArJxjOy1hEMz3fgZgYppDIBZaNjhRWYTRzxLdVwEJLNFVlqcZ5pSnJY1i@kP10WntYw9VTfgx68BgoBCeej2Xt0guoh1r36MAE6ybXGLDRO@Sgpy0UpnPkKBd9xp0iWZBXADOYJ3mnMxQmmBqZZg0iR2jfyJiV56xVlU7CqQoE54bxtixkv4i@TJJX9qqfTnJ0BAdhixOqWpYwOs9pbyx1Ee9SFsmUGnSHMPtbfMsWPAXgkrzERGazzWa5wXlnPbmQVJhcr0WMGEMiRnbVl3FSvT8WXMmNAxxq7E40A1L7OnRca0VuMaDIw7EdonU8sZxTW@EyYU80hf96my35aosGlfli1h8rRrCSMFM5WlOwzXpQ8HyNvYlK1n1npZMTaoqJcEYeJfEY8PJkyn2W7smqQ0D5WxXdJYjBeby4utoOkWqYdNw9GpuZCZD99XihUpHfHphaZqqvH0aTe7@y1sbilVaJhE8E9hUOcoghsGbtAgLwjv9KEKZqtVtPrPlaLU1WEeOOqzbkaMWc@Sow3pdOWoxV45azDuD9aSFFruWFlrs5gzmn8aubelZg3UcUZ/vajH3DOadwUR9vqfFRH1@T4vdnMH801jXPoMJ3n1d7Y7Xk1GPMcd2@D7H0fDu2kIvjsfrq8Dq1GsFWscOBVrDjgRaxw4FWsOOBFrHDgVax7wDwdQxKdAbHeZ3RdO0ufiulI0W86Q0tNghEXXsWtCoiycFgzTqsB7nwevo9jlc9I7d6Wkxl2OuLhekXNIvL8Mnw@D/RdSFJX7j1eUlL0lx4bfUomVs/wE
  • Imagine you have only one byte (8 bits) to store a value, but need to store values from $0$ to $4032$. Impossible, until you are also told that an error of 1/64 of the exact value does not matter.
  • With that knowledge, you decide to design a floating point integer type, which is defined as follows:
  • * All representable numbers are of the form $m\cdot 2^e$, where $m$ is the mantissa and $e$ is the exponent. There are no NaNs and no infinities.
  • * The upper three bits of the byte are the exponent, the lower five are the mantissa. Since no negative numbers are stored, no sign bit is needed.
  • I'll refer to the three bit number represented by the exponent bits as $E$, and the 5 bit value represented by the mantissa bits as $M$
  • * If $E=0$, then $e=0$ and $m=M$ (“denormalized integer”).
  • * Otherwise, $e=E-1$ and $m=M+32$
  • Your task is now to write code that converts a positive integer to that floating point format and back. In particular:
  • * Encoding:
  • - Your code takes an integer $0\le n\le 4032$, and outputs
  • an integer $0\le r\le 255$. You can assume that the input
  • integer is always in the allowed range (that is, no overflow
  • handling is required)
  • - If $n$ can be represented exactly, $r$ is the
  • corresponding representation.
  • - Otherwise, if $n$ is exactly in the middle of two
  • consecutive exactly representable numbers, $r$ is the
  • choice with the even mantissa.
  • - Otherwise, $r$ is the representation of the closest
  • exactly representable number.
  • * Decoding:
  • Your code takes an integer $0\le r\le 255$ and outputs
  • the integer $0\le n\le 4032$ it represents.
  • You can write either two separate routines for encoding and decoding, or a single routine that takes an extra argument telling whether to encode or decode. Here “routine” refers to any form of executable code taking input and giving output (program, function, macro, …).
  • This is code golf, the shortest code wins. If you choose separate encoding and decoding routines, the score is the sum of the sizes of the encoding and decoding routines.
  • An ungolfed (an unoptimized) Python implementation is available [here][Python implementation]
  • Test cases:
  • Encoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 64
  • 66 65
  • 67 66
  • 68 66
  • 69 66
  • 70 67
  • 123 94
  • 124 94
  • 125 94
  • 126 95
  • 127 96
  • 128 96
  • 129 96
  • 130 96
  • 131 97
  • 256 128
  • 2021 223
  • 4032 255
  • Decoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 66
  • 66 68
  • 93 122
  • 94 124
  • 95 126
  • 96 128
  • 97 132
  • 128 256
  • 160 512
  • 223 2016
  • 224 2048
  • 255 4032
  • [Python implementation]: https://tio.run/##lVZdb9owFH3Pr7jqtCmhVAohoQ0qSGxjElrTTYOHalWFUmJGpuBEibNuYv3t7NqOIQGDtAiZ2Of6fp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fUomVs/wE
#2: Post edited by user avatar celtschk‭ · 2021-09-25T10:07:49Z (over 3 years ago)
  • Imagine you have only one byte (8 bits) to store a value, but need to store values from $0$ to $4032$. Impossible, until you are also told that an error of 1/64 of the exact value does not matter.
  • With that knowledge, you decide to design a floating point integer type, which is defined as follows:
  • * All representable numbers are of the form $m\cdot 2^e$, where $m$ is the mantissa and $e$ is the exponent. There are no NaNs and no infinities.
  • * The upper three bits of the byte are the exponent, the lower five are the mantissa. Since no negative numbers are stored, no sign bit is needed.
  • I'll refer to the three bit number represented by the exponent bits as $E$, and the 5 bit value represented by the mantissa bits as $M$
  • * If $E=0$, then $e=0$ and $m=M$ (“denormalized integer”).
  • * Otherwise, $e=E-1$ and $m=M+32$
  • Your task is now to write code that converts a positive integer to that floating point format and back. In particular:
  • * Encoding:
  • - Your code takes an integer $0\le n\le 1984$, and outputs
  • an integer $0\le r\le 255$. You can assume that the input
  • integer is always in the allowed range (that is, no error
  • handling is required)
  • - If $n$ can be represented exactly, $r$ is the
  • corresponding representation.
  • - Otherwise, if $n$ is exactly in the middle of two
  • consecutive exactly representable numbers, $r$ is the
  • choice with the even mantissa.
  • - Otherwise, $r$ is the representation of the closest
  • exactly representable number.
  • * Decoding:
  • Your code takes an integer $0\le r\le 255$ and outputs
  • the integer $0\le n\le 4032$ it represents.
  • You can write either two separate routines for encoding and decoding, or a single routine that takes an extra argument telling whether to encode or decode. Here “routine” refers to any form of executable code taking input and giving output (program, function, macro, …).
  • This is code golf, the shortest code wins. If you choose separate encoding and decoding routines, the score is the sum of the sizes of the encoding and decoding routines.
  • An ungolfed (an unoptimized) Python implementation is available [here][Python implementation]
  • Test cases:
  • Encoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 64
  • 66 65
  • 67 66
  • 68 66
  • 69 66
  • 70 67
  • 123 94
  • 124 94
  • 125 94
  • 126 95
  • 127 96
  • 128 96
  • 129 96
  • 130 96
  • 131 97
  • 256 128
  • 2021 223
  • 4032 255
  • Decoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 66
  • 66 68
  • 93 122
  • 94 124
  • 95 126
  • 96 128
  • 97 132
  • 128 256
  • 160 512
  • 223 2016
  • 224 2048
  • 255 4032
  • [Python implementation]: https://tio.run/##lVZdb9owFH3Pr7jqtCmhVAohoQ0qSGxjElrTTYOHalWFUmJGpuBEibNuYv3t7NqOIQGDtAiZ2Of6fp1jQ/aHrVLa3W6D0f1sMp2O5u8nsykMwDN2K8Fo@hlXHGi1oGl2BR0jmNzP7798C0Z3k@/jj1o7dPUwHz@MPsw43GruMAwjIksgdJFGxKR9iCmz4GrIv/sG4BMvgcItNLdJiD85YWVOgRpi5Q1EhJF8HVMCbEWA/M5SSigTIMH4tnh7WcUJQbdDnlCLAM@4ynHvmcDlACsU82W4YGkO0lyFKrIkZkDL9TPJeb4psLyki5CRCNYhZXFRhBDSCJK0YPAcs0Js5LM5n6E7Cu/ArJxjOy1hEMz3fgZgYppDIBZaNjhRWYTRzxLdVwEJLNFVlqcZ5pSnJY1i@kP10WntYw9VTfgx68BgoBCeej2Xt0guoh1r36MAE6ybXGLDRO@Sgpy0UpnPkKBd9xp0iWZBXADOYJ3mnMxQmmBqZZg0iR2jfyJiV56xVlU7CqQoE54bxtixkv4i@TJJX9qqfTnJ0BAdhixOqWpYwOs9pbyx1Ee9SFsmUGnSHMPtbfMsWPAXgkrzERGazzWa5wXlnPbmQVJhcr0WMGEMiRnbVl3FSvT8WXMmNAxxq7E40A1L7OnRca0VuMaDIw7EdonU8sZxTW@EyYU80hf96my35aosGlfli1h8rRrCSMFM5WlOwzXpQ8HyNvYlK1n1npZMTaoqJcEYeJfEY8PJkyn2W7smqQ0D5WxXdJYjBeby4utoOkWqYdNw9GpuZCZD99XihUpHfHphaZqqvH0aTe7@y1sbilVaJhE8E9hUOcoghsGbtAgLwjv9KEKZqtVtPrPlaLU1WEeOOqzbkaMWc@Sow3pdOWoxV45azDuD9aSFFruWFlrs5gzmn8aubelZg3UcUZ/vajH3DOadwUR9vqfFRH1@T4vdnMH801jXPoMJ3n1d7Y7Xk1GPMcd2@D7H0fDu2kIvjsfrq8Dq1GsFWscOBVrDjgRaxw4FWsOOBFrHDgVax7wDwdQxKdAbHeZ3RdO0ufiulI0W86Q0tNghEXXsWtCoiycFgzTqsB7nwevo9jlc9I7d6Wkxl2OuLhekXNIvL8Mnw@D/RdSFJX7j1eUlL0lx4bfUomVs/wE
  • Imagine you have only one byte (8 bits) to store a value, but need to store values from $0$ to $4032$. Impossible, until you are also told that an error of 1/64 of the exact value does not matter.
  • With that knowledge, you decide to design a floating point integer type, which is defined as follows:
  • * All representable numbers are of the form $m\cdot 2^e$, where $m$ is the mantissa and $e$ is the exponent. There are no NaNs and no infinities.
  • * The upper three bits of the byte are the exponent, the lower five are the mantissa. Since no negative numbers are stored, no sign bit is needed.
  • I'll refer to the three bit number represented by the exponent bits as $E$, and the 5 bit value represented by the mantissa bits as $M$
  • * If $E=0$, then $e=0$ and $m=M$ (“denormalized integer”).
  • * Otherwise, $e=E-1$ and $m=M+32$
  • Your task is now to write code that converts a positive integer to that floating point format and back. In particular:
  • * Encoding:
  • - Your code takes an integer $0\le n\le 4032$, and outputs
  • an integer $0\le r\le 255$. You can assume that the input
  • integer is always in the allowed range (that is, no error
  • handling is required)
  • - If $n$ can be represented exactly, $r$ is the
  • corresponding representation.
  • - Otherwise, if $n$ is exactly in the middle of two
  • consecutive exactly representable numbers, $r$ is the
  • choice with the even mantissa.
  • - Otherwise, $r$ is the representation of the closest
  • exactly representable number.
  • * Decoding:
  • Your code takes an integer $0\le r\le 255$ and outputs
  • the integer $0\le n\le 4032$ it represents.
  • You can write either two separate routines for encoding and decoding, or a single routine that takes an extra argument telling whether to encode or decode. Here “routine” refers to any form of executable code taking input and giving output (program, function, macro, …).
  • This is code golf, the shortest code wins. If you choose separate encoding and decoding routines, the score is the sum of the sizes of the encoding and decoding routines.
  • An ungolfed (an unoptimized) Python implementation is available [here][Python implementation]
  • Test cases:
  • Encoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 64
  • 66 65
  • 67 66
  • 68 66
  • 69 66
  • 70 67
  • 123 94
  • 124 94
  • 125 94
  • 126 95
  • 127 96
  • 128 96
  • 129 96
  • 130 96
  • 131 97
  • 256 128
  • 2021 223
  • 4032 255
  • Decoding:
  • input output
  • 0 0
  • 1 1
  • 31 31
  • 32 32
  • 63 63
  • 64 64
  • 65 66
  • 66 68
  • 93 122
  • 94 124
  • 95 126
  • 96 128
  • 97 132
  • 128 256
  • 160 512
  • 223 2016
  • 224 2048
  • 255 4032
  • [Python implementation]: https://tio.run/##lVZdb9owFH3Pr7jqtCmhVAohoQ0qSGxjElrTTYOHalWFUmJGpuBEibNuYv3t7NqOIQGDtAiZ2Of6fp1jQ/aHrVLa3W6D0f1sMp2O5u8nsykMwDN2K8Fo@hlXHGi1oGl2BR0jmNzP7798C0Z3k@/jj1o7dPUwHz@MPsw43GruMAwjIksgdJFGxKR9iCmz4GrIv/sG4BMvgcItNLdJiD85YWVOgRpi5Q1EhJF8HVMCbEWA/M5SSigTIMH4tnh7WcUJQbdDnlCLAM@4ynHvmcDlACsU82W4YGkO0lyFKrIkZkDL9TPJeb4psLyki5CRCNYhZXFRhBDSCJK0YPAcs0Js5LM5n6E7Cu/ArJxjOy1hEMz3fgZgYppDIBZaNjhRWYTRzxLdVwEJLNFVlqcZ5pSnJY1i@kP10WntYw9VTfgx68BgoBCeej2Xt0guoh1r36MAE6ybXGLDRO@Sgpy0UpnPkKBd9xp0iWZBXADOYJ3mnMxQmmBqZZg0iR2jfyJiV56xVlU7CqQoE54bxtixkv4i@TJJX9qqfTnJ0BAdhixOqWpYwOs9pbyx1Ee9SFsmUGnSHMPtbfMsWPAXgkrzERGazzWa5wXlnPbmQVJhcr0WMGEMiRnbVl3FSvT8WXMmNAxxq7E40A1L7OnRca0VuMaDIw7EdonU8sZxTW@EyYU80hf96my35aosGlfli1h8rRrCSMFM5WlOwzXpQ8HyNvYlK1n1npZMTaoqJcEYeJfEY8PJkyn2W7smqQ0D5WxXdJYjBeby4utoOkWqYdNw9GpuZCZD99XihUpHfHphaZqqvH0aTe7@y1sbilVaJhE8E9hUOcoghsGbtAgLwjv9KEKZqtVtPrPlaLU1WEeOOqzbkaMWc@Sow3pdOWoxV45azDuD9aSFFruWFlrs5gzmn8aubelZg3UcUZ/vajH3DOadwUR9vqfFRH1@T4vdnMH801jXPoMJ3n1d7Y7Xk1GPMcd2@D7H0fDu2kIvjsfrq8Dq1GsFWscOBVrDjgRaxw4FWsOOBFrHDgVax7wDwdQxKdAbHeZ3RdO0ufiulI0W86Q0tNghEXXsWtCoiycFgzTqsB7nwevo9jlc9I7d6Wkxl2OuLhekXNIvL8Mnw@D/RdSFJX7j1eUlL0lx4bfUomVs/wE
#1: Initial revision by user avatar celtschk‭ · 2021-09-25T10:05:55Z (over 3 years ago)
Encode and decode floating point integers
Imagine you have only one byte (8 bits) to store a value, but need to store values from $0$ to $4032$. Impossible, until you are also told that an error of 1/64 of the exact value does not matter.

With that knowledge, you decide to design a floating point integer type, which is defined as follows:

  * All representable numbers are of the form $m\cdot 2^e$, where $m$ is the mantissa and $e$ is the exponent. There are no NaNs and no infinities.

  * The upper three bits of the byte are the exponent, the lower five are the mantissa. Since no negative numbers are stored, no sign bit is needed.

    I'll refer to the three bit number represented by the exponent bits as $E$, and the 5 bit value represented by the mantissa bits as $M$

  * If $E=0$, then $e=0$ and $m=M$ (“denormalized integer”).

  * Otherwise, $e=E-1$ and $m=M+32$

Your task is now to write code that converts a positive integer to that floating point format and back. In particular:

  * Encoding:

      - Your code takes an integer $0\le n\le 1984$, and outputs
        an integer $0\le r\le 255$. You can assume that the input
        integer is always in the allowed range (that is, no error
        handling is required)

      - If $n$ can be represented exactly, $r$ is the
        corresponding representation.

      - Otherwise, if $n$ is exactly in the middle of two
        consecutive exactly representable numbers, $r$ is the
        choice with the even mantissa.

      - Otherwise, $r$ is the representation of the closest
        exactly representable number.

  * Decoding:

    Your code takes an integer $0\le r\le 255$ and outputs
    the integer $0\le n\le 4032$ it represents.

You can write either two separate routines for encoding and decoding, or a single routine that takes an extra argument telling whether to encode or decode. Here “routine” refers to any form of executable code taking input and giving output (program, function, macro, …).

This is code golf, the shortest code wins. If you choose separate encoding and decoding routines, the score is the sum of the sizes of the encoding and decoding routines.

An ungolfed (an unoptimized) Python implementation is available [here][Python implementation]

Test cases:

Encoding:

     input  output
        0      0
        1      1
       31     31
       32     32
       63     63
       64     64
       65     64
       66     65
       67     66
       68     66
       69     66
       70     67
      123     94
      124     94
      125     94
      126     95
      127     96
      128     96
      129     96
      130     96
      131     97
      256    128
     2021    223
     4032    255

Decoding:

    input  output
       0     0 
       1     1
      31    31
      32    32
      63    63
      64    64
      65    66
      66    68
      93   122
      94   124
      95   126
      96   128
      97   132
     128   256
     160   512
     223  2016
     224  2048
     255  4032


[Python implementation]: https://tio.run/##lVZdb9owFH3Pr7jqtCmhVAohoQ0qSGxjElrTTYOHalWFUmJGpuBEibNuYv3t7NqOIQGDtAiZ2Of6fp1jQ/aHrVLa3W6D0f1sMp2O5u8nsykMwDN2K8Fo@hlXHGi1oGl2BR0jmNzP7798C0Z3k@/jj1o7dPUwHz@MPsw43GruMAwjIksgdJFGxKR9iCmz4GrIv/sG4BMvgcItNLdJiD85YWVOgRpi5Q1EhJF8HVMCbEWA/M5SSigTIMH4tnh7WcUJQbdDnlCLAM@4ynHvmcDlACsU82W4YGkO0lyFKrIkZkDL9TPJeb4psLyki5CRCNYhZXFRhBDSCJK0YPAcs0Js5LM5n6E7Cu/ArJxjOy1hEMz3fgZgYppDIBZaNjhRWYTRzxLdVwEJLNFVlqcZ5pSnJY1i@kP10WntYw9VTfgx68BgoBCeej2Xt0guoh1r36MAE6ybXGLDRO@Sgpy0UpnPkKBd9xp0iWZBXADOYJ3mnMxQmmBqZZg0iR2jfyJiV56xVlU7CqQoE54b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