-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
FloatHex.pas
924 lines (727 loc) · 31.9 KB
/
FloatHex.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
FloatHex
Small set of functions for conversions of floating point numbers to and
from hexadecimal strings and functions for direct casting between integers
and floats.
Note that when converting to and from type Extended, the strings will be
20 characters long - as if the type is 80bits (10bytes) in size,
irrespective of how it is declared (in 64bit windows programs, it is
usually declared as a simple alias for type double (64bits)).
Version 2.1.1 (2024-04-14)
Last change 2024-04-28
©2015-2024 František Milt
Contacts:
František Milt: [email protected]
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.FloatHex
Dependencies:
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
Float80Utils - github.com/TheLazyTomcat/Lib.Float80Utils
Library AuxExceptions is required only when rebasing local exception classes
(see symbol FloatHex_UseAuxExceptions for details).
Library AuxExceptions might also be required as an indirect dependency.
Indirect dependencies:
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
StrRect - github.com/TheLazyTomcat/Lib.StrRect
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit FloatHex;
{
FloatHex_UseAuxExceptions
If you want library-specific exceptions to be based on more advanced classes
provided by AuxExceptions library instead of basic Exception class, and don't
want to or cannot change code in this unit, you can define global symbol
FloatHex_UseAuxExceptions to achieve this.
}
{$IF Defined(FloatHex_UseAuxExceptions)}
{$DEFINE UseAuxExceptions}
{$IFEND}
//------------------------------------------------------------------------------
{$IFDEF ENDIAN_BIG}
{$MESSAGE FATAL 'Big-endian system not supported'}
{$ENDIF}
{$IFDEF FPC}
{$MODE ObjFPC}
{$INLINE ON}
{$DEFINE CanInline}
{$ELSE}
{$IF CompilerVersion >= 17} // Delphi 2005+
{$DEFINE CanInline}
{$ELSE}
{$UNDEF CanInline}
{$IFEND}
{$ENDIF}
{$H+}
interface
uses
SysUtils,
AuxTypes, Float80Utils{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
type
// library-specific exceptions, unused atm.
EFHException = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
{===============================================================================
--------------------------------------------------------------------------------
Float <-> HexString conversions
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
Float <-> HexString conversions - declaration
===============================================================================}
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Float16
-------------------------------------------------------------------------------}
Function Float16ToHex(Value: Float16): String;
Function HexToFloat16(const HexString: String): Float16;
Function TryHexToFloat16(const HexString: String; out Value: Float16): Boolean;
Function HexToFloat16Def(const HexString: String; const DefaultValue: Float16): Float16;
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Half
-------------------------------------------------------------------------------}
Function HalfToHex(Value: Half): String;{$IFDEF CanInline} inline;{$ENDIF}
Function HexToHalf(const HexString: String): Half;{$IFDEF CanInline} inline;{$ENDIF}
Function TryHexToHalf(const HexString: String; out Value: Half): Boolean;{$IFDEF CanInline} inline;{$ENDIF}
Function HexToHalfDef(const HexString: String; const DefaultValue: Half): Half;{$IFDEF CanInline} inline;{$ENDIF}
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Float32
-------------------------------------------------------------------------------}
Function Float32ToHex(Value: Float32): String;
Function HexToFloat32(const HexString: String): Float32;
Function TryHexToFloat32(const HexString: String; out Value: Float32): Boolean;
Function HexToFloat32Def(const HexString: String; const DefaultValue: Float32): Float32;
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Single
-------------------------------------------------------------------------------}
Function SingleToHex(Value: Single): String;{$IFDEF CanInline} inline;{$ENDIF}
Function HexToSingle(const HexString: String): Single;{$IFDEF CanInline} inline;{$ENDIF}
Function TryHexToSingle(const HexString: String; out Value: Single): Boolean;{$IFDEF CanInline} inline;{$ENDIF}
Function HexToSingleDef(const HexString: String; const DefaultValue: Single): Single;{$IFDEF CanInline} inline;{$ENDIF}
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Float64
-------------------------------------------------------------------------------}
Function Float64ToHex(Value: Float64): String;
Function HexToFloat64(const HexString: String): Float64;
Function TryHexToFloat64(const HexString: String; out Value: Float64): Boolean;
Function HexToFloat64Def(const HexString: String; const DefaultValue: Float64): Float64;
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Double
-------------------------------------------------------------------------------}
Function DoubleToHex(Value: Double): String;{$IFDEF CanInline} inline;{$ENDIF}
Function HexToDouble(const HexString: String): Double;{$IFDEF CanInline} inline;{$ENDIF}
Function TryHexToDouble(const HexString: String; out Value: Double): Boolean;{$IFDEF CanInline} inline;{$ENDIF}
Function HexToDoubleDef(const HexString: String; const DefaultValue: Double): Double;{$IFDEF CanInline} inline;{$ENDIF}
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Float80
-------------------------------------------------------------------------------}
Function Float80ToHex(Value: Float80): String;
Function HexToFloat80(const HexString: String): Float80;
Function TryHexToFloat80(const HexString: String; out Value: Float80): Boolean;
Function HexToFloat80Def(const HexString: String; const DefaultValue: Float80): Float80;
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Extended
-------------------------------------------------------------------------------}
Function ExtendedToHex(Value: Extended): String;
Function HexToExtended(const HexString: String): Extended;
Function TryHexToExtended(const HexString: String; out Value: Extended): Boolean;
Function HexToExtendedDef(const HexString: String; const DefaultValue: Extended): Extended;
{-------------------------------------------------------------------------------
Float <-> HexString conversions - default float type
-------------------------------------------------------------------------------}
Function FloatToHex(Value: Double): String;{$IFDEF CanInline} inline;{$ENDIF}
Function HexToFloat(const HexString: String): Double;{$IFDEF CanInline} inline;{$ENDIF}
Function TryHexToFloat(const HexString: String; out Value: Double): Boolean;{$IFDEF CanInline} inline;{$ENDIF}
Function HexToFloatDef(const HexString: String; const DefaultValue: Double): Double;{$IFDEF CanInline} inline;{$ENDIF}
{===============================================================================
--------------------------------------------------------------------------------
Float <-> Integer casting
--------------------------------------------------------------------------------
===============================================================================}
{
Following functions are casting the integers to floats and back as if they
would occupy the same memory location, they are NOT converting them.
The result will have the same bit pattern as the input parameter, which might
lead to exceptions if you happen to produce SNaN from an integer - be aware
of that and do not blindly pass some random integers.
Note that unsigned integers are used to reduce potential problems with
negative values (don't ask, accept it >:/).
}
{===============================================================================
Float <-> Integer casting - declaration
===============================================================================}
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Float16
-------------------------------------------------------------------------------}
Function CastToFloat16(Value: UInt16): Float16;
Function CastFromFloat16(Value: Float16): UInt16;
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Half
-------------------------------------------------------------------------------}
Function CastToHalf(Value: UInt16): Half;{$IFDEF CanInline} inline;{$ENDIF}
Function CastFromHalf(Value: Half): UInt16;{$IFDEF CanInline} inline;{$ENDIF}
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Float32
-------------------------------------------------------------------------------}
Function CastToFloat32(Value: UInt32): Float32;
Function CastFromFloat32(Value: Float32): UInt32;
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Single
-------------------------------------------------------------------------------}
Function CastToSingle(Value: UInt32): Single;{$IFDEF CanInline} inline;{$ENDIF}
Function CastFromSingle(Value: Single): UInt32;{$IFDEF CanInline} inline;{$ENDIF}
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Float64
-------------------------------------------------------------------------------}
Function CastToFloat64(Value: UInt64): Float64;
Function CastFromFloat64(Value: Float64): UInt64;
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Double
-------------------------------------------------------------------------------}
Function CastToDouble(Value: UInt64): Double;{$IFDEF CanInline} inline;{$ENDIF}
Function CastFromDouble(Value: Double): UInt64;{$IFDEF CanInline} inline;{$ENDIF}
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Float80
-------------------------------------------------------------------------------}
Function CastToFloat80(const Value): Float80; overload;
Function CastToFloat80(ExponentWithSign: UInt16; Mantissa: UInt64): Float80; overload;
procedure CastFromFloat80(Value: Float80; out Result); overload;
procedure CastFromFloat80(Value: Float80; out ExponentWithSign: UInt16; out Mantissa: UInt64); overload;
Function CastFromFloat80(Value: Float80): TFloat80Overlay; overload;
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Extended
-------------------------------------------------------------------------------}
{
WARNING - in following functions, the integer input/output is ALWAYS 80bits
(10 bytes) wide, irrespective of how is the type Extended declared
(conversion to/from 80bit to 64bit float is done automatically when
needed).
If you want to have integer in/out to be the true size of Extended,
use functions NativeCastToExtended and NativeCastFromExtended.
}
Function CastToExtended(const Value): Extended; overload;
Function CastToExtended(ExponentWithSign: UInt16; Mantissa: UInt64): Extended; overload;
procedure CastFromExtended(Value: Extended; out Result); overload;
procedure CastFromExtended(Value: Extended; out ExponentWithSign: UInt16; out Mantissa: UInt64); overload;
Function CastFromExtended(Value: Extended): TFloat80Overlay; overload;
{
In following two functions, the size of input/output buffer equals to actual
size of type Extended, whatever its declaration may be.
}
Function NativeCastToExtended(const Value): Extended; overload;
procedure NativeCastFromExtended(Value: Extended; out Result); overload;
{-------------------------------------------------------------------------------
Float <-> Integer casting - default float type
-------------------------------------------------------------------------------}
Function CastToFloat(Value: UInt64): Double;{$IFDEF CanInline} inline;{$ENDIF}
Function CastFromFloat(Value: Double): UInt64;{$IFDEF CanInline} inline;{$ENDIF}
implementation
// do not place higher (FPC does not like it)
{$IF SizeOf(Extended) = 8}
{$DEFINE Extended64}
{$ELSEIF SizeOf(Extended) = 10}
{$UNDEF Extended64}
{$ELSE}
{$MESSAGE FATAL 'Unsupported platform, type extended must be 8 or 10 bytes.'}
{$IFEND}
{===============================================================================
--------------------------------------------------------------------------------
Float <-> HexString conversions
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
Float <-> HexString conversions - auxiliary routines
===============================================================================}
Function RectifyHexString(const Str: String; RequiredLength: Integer): String;
Function StartsWithHexMark: Boolean;
begin
If Length(Str) > 0 then
Result := Str[1] = '$'
else
Result := False;
end;
begin
If not StartsWithHexMark then
Result := '$' + Str
else
Result := Str;
Inc(RequiredLength);
If Length(Result) <> RequiredLength then
begin
If Length(Result) < RequiredLength then
Result := Result + StringOfChar('0',RequiredLength - Length(Result))
else
Result := Copy(Result,1,RequiredLength);
end;
end;
{===============================================================================
Float <-> HexString conversions - implementation
===============================================================================}
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Float16
-------------------------------------------------------------------------------}
Function Float16ToHex(Value: Float16): String;
var
Overlay: UInt16 absolute Value;
begin
Result := IntToHex(Overlay,4);
end;
//------------------------------------------------------------------------------
Function HexToFloat16(const HexString: String): Float16;
var
Overlay: UInt16 absolute Result;
begin
Overlay := UInt16(StrToInt(RectifyHexString(HexString,4)));
end;
//------------------------------------------------------------------------------
Function TryHexToFloat16(const HexString: String; out Value: Float16): Boolean;
begin
try
Value := HexToFloat16(HexString);
Result := True;
except
Result := False;
end;
end;
//------------------------------------------------------------------------------
Function HexToFloat16Def(const HexString: String; const DefaultValue: Float16): Float16;
begin
If not TryHexToFloat16(HexString,Result) then
Result := DefaultValue;
end;
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Half
-------------------------------------------------------------------------------}
Function HalfToHex(Value: Half): String;
begin
Result := Float16ToHex(Value);
end;
//------------------------------------------------------------------------------
Function HexToHalf(const HexString: String): Half;
begin
Result := HexToFloat16(HexString);
end;
//------------------------------------------------------------------------------
Function TryHexToHalf(const HexString: String; out Value: Half): Boolean;
begin
Result := TryHexToFloat16(HexString,Value);
end;
//------------------------------------------------------------------------------
Function HexToHalfDef(const HexString: String; const DefaultValue: Half): Half;
begin
Result := HexToFloat16Def(HexString,DefaultValue);
end;
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Float32
-------------------------------------------------------------------------------}
Function Float32ToHex(Value: Float32): String;
var
Overlay: UInt32 absolute Value;
begin
Result := IntToHex(Overlay,8);
end;
//------------------------------------------------------------------------------
Function HexToFloat32(const HexString: String): Float32;
var
Overlay: UInt32 absolute Result;
begin
Overlay := UInt32(StrToInt(RectifyHexString(HexString,8)));
end;
//------------------------------------------------------------------------------
Function TryHexToFloat32(const HexString: String; out Value: Float32): Boolean;
begin
try
Value := HexToFloat32(HexString);
Result := True;
except
Result := False;
end;
end;
//------------------------------------------------------------------------------
Function HexToFloat32Def(const HexString: String; const DefaultValue: Float32): Float32;
begin
If not TryHexToFloat32(HexString,Result) then
Result := DefaultValue;
end;
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Single
-------------------------------------------------------------------------------}
Function SingleToHex(Value: Single): String;
begin
Result := Float32ToHex(Value);
end;
//------------------------------------------------------------------------------
Function HexToSingle(const HexString: String): Single;
begin
Result := HexToFloat32(HexString);
end;
//------------------------------------------------------------------------------
Function TryHexToSingle(const HexString: String; out Value: Single): Boolean;
begin
Result := TryHexToFloat32(HexString,Value);
end;
//------------------------------------------------------------------------------
Function HexToSingleDef(const HexString: String; const DefaultValue: Single): Single;
begin
Result := HexToFloat32Def(HexString,DefaultValue);
end;
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Float64
-------------------------------------------------------------------------------}
Function Float64ToHex(Value: Float64): String;
var
Overlay: UInt64 absolute Value;
begin
Result := IntToHex(Overlay,16);
end;
//------------------------------------------------------------------------------
Function HexToFloat64(const HexString: String): Float64;
var
Overlay: Int64 absolute Result;
begin
Overlay := StrToInt64(RectifyHexString(HexString,16));
end;
//------------------------------------------------------------------------------
Function TryHexToFloat64(const HexString: String; out Value: Float64): Boolean;
begin
try
Value := HexToFloat64(HexString);
Result := True;
except
Result := False;
end;
end;
//------------------------------------------------------------------------------
Function HexToFloat64Def(const HexString: String; const DefaultValue: Float64): Float64;
begin
If not TryHexToFloat64(HexString,Result) then
Result := DefaultValue;
end;
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Double
-------------------------------------------------------------------------------}
Function DoubleToHex(Value: Double): String;
begin
Result := Float64ToHex(Value);
end;
//------------------------------------------------------------------------------
Function HexToDouble(const HexString: String): Double;
begin
Result := HexToFloat64(HexString);
end;
//------------------------------------------------------------------------------
Function TryHexToDouble(const HexString: String; out Value: Double): Boolean;
begin
Result := TryHexToFloat64(HexString,Value);
end;
//------------------------------------------------------------------------------
Function HexToDoubleDef(const HexString: String; const DefaultValue: Double): Double;
begin
Result := HexToFloat64Def(HexString,DefaultValue);
end;
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Float80
-------------------------------------------------------------------------------}
Function Float80ToHex(Value: Float80): String;
var
Overlay: TFloat80Overlay absolute Value;
begin
Result := IntToHex(Overlay.Part_16,4) + IntToHex(Overlay.Part_64,16);
end;
//------------------------------------------------------------------------------
Function HexToFloat80(const HexString: String): Float80;
var
Temp: String;
Overlay: TFloat80Overlay absolute Result;
begin
Temp := RectifyHexString(HexString,20);
Overlay.Part_16 := UInt16(StrToInt(Copy(Temp,1,5)));
Overlay.Part_64 := UInt64(StrToInt64('$' + Copy(Temp,6,16)));
end;
//------------------------------------------------------------------------------
Function TryHexToFloat80(const HexString: String; out Value: Float80): Boolean;
begin
try
Value := HexToFloat80(HexString);
Result := True;
except
Result := False;
end;
end;
//------------------------------------------------------------------------------
Function HexToFloat80Def(const HexString: String; const DefaultValue: Float80): Float80;
begin
If not TryHexToFloat80(HexString,Result) then
Result := DefaultValue;
end;
{-------------------------------------------------------------------------------
Float <-> HexString conversions - type Extended
-------------------------------------------------------------------------------}
Function ExtendedToHex(Value: Extended): String;
var
Overlay: TFloat80Overlay {$IFNDEF Extended64}absolute Value{$ENDIF};
begin
{$IFDEF Extended64}
Float64ToFloat80(@Value,@Overlay);
{$ENDIF}
Result := IntToHex(Overlay.Part_16,4) + IntToHex(Overlay.Part_64,16);
end;
//------------------------------------------------------------------------------
Function HexToExtended(const HexString: String): Extended;
var
Temp: String;
Overlay: TFloat80Overlay {$IFNDEF Extended64}absolute Result{$ENDIF};
begin
Temp := RectifyHexString(HexString,20);
Overlay.Part_16 := UInt16(StrToInt(Copy(Temp,1,5)));
Overlay.Part_64 := UInt64(StrToInt64('$' + Copy(Temp,6,16)));
{$IFDEF Extended64}
Float80ToFloat64(@Overlay,@Result);
{$ENDIF}
end;
//------------------------------------------------------------------------------
Function TryHexToExtended(const HexString: String; out Value: Extended): Boolean;
begin
try
Value := HexToExtended(HexString);
Result := True;
except
Result := False;
end;
end;
//------------------------------------------------------------------------------
Function HexToExtendedDef(const HexString: String; const DefaultValue: Extended): Extended;
begin
If not TryHexToExtended(HexString,Result) then
Result := DefaultValue;
end;
{-------------------------------------------------------------------------------
Float <-> HexString conversions - default float type
-------------------------------------------------------------------------------}
Function FloatToHex(Value: Double): String;
begin
Result := DoubleToHex(Value);
end;
//------------------------------------------------------------------------------
Function HexToFloat(const HexString: String): Double;
begin
Result := HexToDouble(HexString);
end;
//------------------------------------------------------------------------------
Function TryHexToFloat(const HexString: String; out Value: Double): Boolean;
begin
Result := TryHexToDouble(HexString,Value);
end;
//------------------------------------------------------------------------------
Function HexToFloatDef(const HexString: String; const DefaultValue: Double): Double;
begin
Result := HexToDoubleDef(HexString,DefaultValue);
end;
{===============================================================================
--------------------------------------------------------------------------------
Float <-> Integer casting
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
Float <-> Integer casting - implementation
===============================================================================}
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Float16
-------------------------------------------------------------------------------}
Function CastToFloat16(Value: UInt16): Float16;
var
Temp: UInt16 absolute Result;
begin
Temp := Value;
end;
//------------------------------------------------------------------------------
Function CastFromFloat16(Value: Float16): UInt16;
var
Temp: Float16 absolute Result;
begin
Temp := Value;
end;
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Half
-------------------------------------------------------------------------------}
Function CastToHalf(Value: UInt16): Half;
begin
Result := CastToFloat16(Value);
end;
//------------------------------------------------------------------------------
Function CastFromHalf(Value: Half): UInt16;
begin
Result := CastFromFloat16(Value);
end;
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Float32
-------------------------------------------------------------------------------}
Function CastToFloat32(Value: UInt32): Float32;
var
Temp: UInt32 absolute Result;
begin
Temp := Value;
end;
//------------------------------------------------------------------------------
Function CastFromFloat32(Value: Float32): UInt32;
var
Temp: Float32 absolute Result;
begin
Temp := Value;
end;
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Single
-------------------------------------------------------------------------------}
Function CastToSingle(Value: UInt32): Single;
begin
Result := CastToFloat32(Value);
end;
//------------------------------------------------------------------------------
Function CastFromSingle(Value: Single): UInt32;
begin
Result := CastFromFloat32(Value);
end;
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Float64
-------------------------------------------------------------------------------}
Function CastToFloat64(Value: UInt64): Float64;
var
Temp: UInt64 absolute Result;
begin
Temp := Value;
end;
//------------------------------------------------------------------------------
Function CastFromFloat64(Value: Float64): UInt64;
var
Temp: Float64 absolute Result;
begin
Temp := Value;
end;
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Double
-------------------------------------------------------------------------------}
Function CastToDouble(Value: UInt64): Double;
begin
Result := CastToFloat64(Value);
end;
//------------------------------------------------------------------------------
Function CastFromDouble(Value: Double): UInt64;
begin
Result := CastFromFloat64(Value);
end;
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Float80
-------------------------------------------------------------------------------}
Function CastToFloat80(const Value): Float80;
begin
Result := Float80(Value);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function CastToFloat80(ExponentWithSign: UInt16; Mantissa: UInt64): Float80;
begin
TFloat80Overlay(Result).SignExponent := ExponentWithSign;
TFloat80Overlay(Result).Mantissa := Mantissa;
end;
//------------------------------------------------------------------------------
procedure CastFromFloat80(Value: Float80; out Result);
begin
Float80(Result) := Value;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure CastFromFloat80(Value: Float80; out ExponentWithSign: UInt16; out Mantissa: UInt64);
begin
ExponentWithSign := TFloat80Overlay(Value).SignExponent;
Mantissa := TFloat80Overlay(Value).Mantissa;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function CastFromFloat80(Value: Float80): TFloat80Overlay;
begin
Result := TFloat80Overlay(Value);
end;
{-------------------------------------------------------------------------------
Float <-> Integer casting - type Extended
-------------------------------------------------------------------------------}
Function CastToExtended(const Value): Extended;
begin
{$IFDEF Extended64}
Float80ToFloat64(@Value,@Result);
{$ELSE}
Result := Extended(Value);
{$ENDIF}
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function CastToExtended(ExponentWithSign: UInt16; Mantissa: UInt64): Extended;
var
Temp: TFloat80Overlay;
begin
Temp.SignExponent := ExponentWithSign;
Temp.Mantissa := Mantissa;
{$IFDEF Extended64}
Float80ToFloat64(@Temp,@Result);
{$ELSE}
Result := Extended(Temp);
{$ENDIF}
end;
//------------------------------------------------------------------------------
procedure CastFromExtended(Value: Extended; out Result);
begin
{$IFDEF Extended64}
Float64ToFloat80(@Value,@Result);
{$ELSE}
Extended(Result) := Value;
{$ENDIF}
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure CastFromExtended(Value: Extended; out ExponentWithSign: UInt16; out Mantissa: UInt64);
var
Temp: TFloat80Overlay;
begin
{$IFDEF Extended64}
Float64ToFloat80(@Value,@Temp);
{$ELSE}
Temp := TFloat80Overlay(Value);
{$ENDIF}
ExponentWithSign := Temp.SignExponent;
Mantissa := Temp.Mantissa;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function CastFromExtended(Value: Extended): TFloat80Overlay;
begin
{$IFDEF Extended64}
Float64ToFloat80(@Value,@Result);
{$ELSE}
Result := TFloat80Overlay(Value);
{$ENDIF}
end;
//------------------------------------------------------------------------------
Function NativeCastToExtended(const Value): Extended;
begin
Result := Extended(Value);
end;
//------------------------------------------------------------------------------
procedure NativeCastFromExtended(Value: Extended; out Result);
begin
Extended(Result) := Value;
end;
{-------------------------------------------------------------------------------
Float <-> Integer casting - default float type
-------------------------------------------------------------------------------}
Function CastToFloat(Value: UInt64): Double;
begin
Result := CastToFloat64(Value);
end;
//------------------------------------------------------------------------------
Function CastFromFloat(Value: Double): UInt64;
begin
Result := CastFromFloat64(Value);
end;
end.