jaulib v1.3.0
Jau Support Library (C++, Java, ..)
ValueConv.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2012 Gothel Software e.K.
5 * Copyright (c) 2012 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26package org.jau.util;
27
28/**
29 * Utility class providing simple signed and unsigned primitive value conversions
30 * for byte, short, int, float and double.
31 * <p>
32 * Non float to non float conversions are handled via float or double,
33 * depending on the value range.
34 * </p>
35 */
36public class ValueConv {
37 public static final byte float_to_byte(final float v, final boolean dSigned) {
38 // lossy
39 if( dSigned ) {
40 return (byte) ( v * ( v > 0 ? 127.0f : 128.0f ) );
41 } else {
42 return (byte) ( v * 255.0f );
43 }
44 }
45 public static final short float_to_short(final float v, final boolean dSigned) {
46 if( dSigned ) {
47 return (short) ( v * ( v > 0 ? 32767.0f : 32768.0f ) );
48 } else {
49 return (short) ( v * 65535.0f );
50 }
51 }
52 public static final int float_to_int(final float v, final boolean dSigned) {
53 // float significand 0x007fffff
54 // double significand 0x000fffffffffffffL
55 // int min = 0x80000000 = -2147483648
56 // int max = 0x7fffffff = +2147483647
57 if( dSigned ) {
58 return (int) ( v * ( v > 0 ? 2147483647.0 : 2147483648.0 ) );
59 } else {
60 return (int) (long) ( v * 4294967295.0 );
61 }
62 }
63
64 public static final byte double_to_byte(final double v, final boolean dSigned) {
65 // lossy
66 if( dSigned ) {
67 return (byte) ( v * ( v > 0 ? 127.0 : 128.0 ) );
68 } else {
69 return (byte) ( v * 255.0 );
70 }
71 }
72 public static final short double_to_short(final double v, final boolean dSigned) {
73 // lossy
74 if( dSigned ) {
75 return (short) ( v * ( v > 0 ? 32767.0 : 32768.0 ) );
76 } else {
77 return (short) ( v * 65535.0 );
78 }
79 }
80 public static final int double_to_int(final double v, final boolean dSigned) {
81 // lossy
82 if( dSigned ) {
83 return (int) ( v * ( v > 0 ? 2147483647.0 : 2147483648.0 ) );
84 } else {
85 return (int) (long) ( v * 4294967295.0 );
86 }
87 }
88
89 public static final float byte_to_float(final byte v, final boolean sSigned) {
90 if( sSigned ) {
91 return (v & 0xff) / ( v > 0 ? 127.0f : -128.0f ) ;
92 } else {
93 return (v & 0xff) / 255.0f ;
94 }
95 }
96 public static final double byte_to_double(final byte v, final boolean sSigned) {
97 if( sSigned ) {
98 return (v & 0xff) / ( v > 0 ? 127.0 : -128.0 ) ;
99 } else {
100 return (v & 0xff) / 255.0 ;
101 }
102 }
103 public static final float short_to_float(final short v, final boolean sSigned) {
104 if( sSigned ) {
105 return (v & 0xffff) / ( v > 0 ? 32767.0f : -32768.0f ) ;
106 } else {
107 return (v & 0xffff) / 65535.0f ;
108 }
109 }
110 public static final double short_to_double(final short v, final boolean sSigned) {
111 // lossy
112 if( sSigned ) {
113 return (v & 0xffff) / ( v > 0 ? 32767.0 : -32768.0 ) ;
114 } else {
115 return (v & 0xffff) / 65535.0 ;
116 }
117 }
118 public static final float int_to_float(final int v, final boolean sSigned) {
119 // lossy
120 // float significand 0x007fffff
121 // double significand 0x000fffffffffffffL
122 // int min = 0x80000000 = -2147483648
123 // int max = 0x7fffffff = +2147483647
124 if( sSigned ) {
125 return (float) ( v / ( v > 0 ? 2147483647.0 : 2147483648.0 ) );
126 } else {
127 return (float) ( (v & 0xffffffffL) / 4294967295.0 );
128 }
129 }
130 public static final double int_to_double(final int v, final boolean sSigned) {
131 if( sSigned ) {
132 return v / ( v > 0 ? 2147483647.0 : 2147483648.0 ) ;
133 } else {
134 return (v & 0xffffffffL) / 4294967295.0 ;
135 }
136 }
137
138 public static final short byte_to_short(final byte v, final boolean sSigned, final boolean dSigned) {
139 return float_to_short(byte_to_float(v, sSigned), dSigned);
140 }
141 public static final int byte_to_int(final byte v, final boolean sSigned, final boolean dSigned) {
142 return float_to_int(byte_to_float(v, sSigned), dSigned);
143 }
144
145 public static final byte short_to_byte(final short v, final boolean sSigned, final boolean dSigned) {
146 return float_to_byte(short_to_float(v, sSigned), dSigned);
147 }
148 public static final int short_to_int(final short v, final boolean sSigned, final boolean dSigned) {
149 return float_to_int(short_to_float(v, sSigned), dSigned);
150 }
151
152 public static final byte int_to_byte(final int v, final boolean sSigned, final boolean dSigned) {
153 return float_to_byte(int_to_float(v, sSigned), dSigned);
154 }
155 public static final short int_to_short(final int v, final boolean sSigned, final boolean dSigned) {
156 return float_to_short(int_to_float(v, sSigned), dSigned);
157 }
158}
Utility class providing simple signed and unsigned primitive value conversions for byte,...
Definition: ValueConv.java:36
static final byte short_to_byte(final short v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:145
static final byte int_to_byte(final int v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:152
static final float short_to_float(final short v, final boolean sSigned)
Definition: ValueConv.java:103
static final short byte_to_short(final byte v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:138
static final int double_to_int(final double v, final boolean dSigned)
Definition: ValueConv.java:80
static final short float_to_short(final float v, final boolean dSigned)
Definition: ValueConv.java:45
static final int float_to_int(final float v, final boolean dSigned)
Definition: ValueConv.java:52
static final double int_to_double(final int v, final boolean sSigned)
Definition: ValueConv.java:130
static final byte float_to_byte(final float v, final boolean dSigned)
Definition: ValueConv.java:37
static final int short_to_int(final short v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:148
static final float byte_to_float(final byte v, final boolean sSigned)
Definition: ValueConv.java:89
static final short int_to_short(final int v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:155
static final short double_to_short(final double v, final boolean dSigned)
Definition: ValueConv.java:72
static final double short_to_double(final short v, final boolean sSigned)
Definition: ValueConv.java:110
static final byte double_to_byte(final double v, final boolean dSigned)
Definition: ValueConv.java:64
static final int byte_to_int(final byte v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:141
static final double byte_to_double(final byte v, final boolean sSigned)
Definition: ValueConv.java:96
static final float int_to_float(final int v, final boolean sSigned)
Definition: ValueConv.java:118