00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef CEYLAN_MATHS_BASIC_H_
00028 #define CEYLAN_MATHS_BASIC_H_
00029
00030
00031
00032 #include "CeylanException.h"
00033 #include "CeylanTypes.h"
00034 #include "CeylanFunctor.h"
00035
00036
00037 #include <string>
00038
00039
00040
00041 namespace Ceylan
00042 {
00043
00044
00046 namespace Maths
00047 {
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00082 typedef Ceylan::Sint32 IntegerData ;
00083
00084
00085
00093 typedef Ceylan::Float32 Real ;
00094
00095
00096
00103 typedef Ceylan::Float32 AngleInDegrees ;
00104
00105
00106
00113 typedef Ceylan::Float32 AngleInRadians ;
00114
00115
00116
00122 typedef Ceylan::Uint8 Percentage ;
00123
00124
00125
00135 typedef Ceylan::Uint32 Hertz ;
00136
00137
00138
00143 typedef Ceylan::Float32 Ratio ;
00144
00145
00146
00147
00149 class CEYLAN_DLL MathsException : public Ceylan::Exception
00150 {
00151
00152 public:
00153
00154 explicit MathsException( const std::string & message ) ;
00155
00156 virtual ~MathsException() throw() ;
00157
00158 } ;
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00176 extern CEYLAN_DLL const Ceylan::LongFloat E ;
00177
00178
00179
00185 extern CEYLAN_DLL const Ceylan::LongFloat Log2E ;
00186
00187
00188
00194 extern CEYLAN_DLL const Ceylan::LongFloat Log10E ;
00195
00196
00197
00203 extern CEYLAN_DLL const Ceylan::LongFloat LogE2 ;
00204
00205
00206
00212 extern CEYLAN_DLL const Ceylan::LongFloat LogE10 ;
00213
00214
00215
00216
00217
00218
00219
00225 extern CEYLAN_DLL const Ceylan::LongFloat Pi ;
00226
00227
00228
00234 extern CEYLAN_DLL const Ceylan::LongFloat Pi_div_2 ;
00235
00236
00237
00243 extern CEYLAN_DLL const Ceylan::LongFloat Pi_div_4 ;
00244
00245
00246
00252 extern CEYLAN_DLL const Ceylan::LongFloat One_div_Pi ;
00253
00254
00255
00261 extern CEYLAN_DLL const Ceylan::LongFloat Two_div_Pi ;
00262
00263
00264
00270 extern CEYLAN_DLL const Ceylan::LongFloat Two_div_sqrt_Pi ;
00271
00272
00273
00274
00275
00276
00277
00278
00284 extern CEYLAN_DLL const Ceylan::LongFloat Sqrt_2 ;
00285
00286
00287
00293 extern CEYLAN_DLL const Ceylan::LongFloat One_div_sqrt_2 ;
00294
00295
00296
00302 extern CEYLAN_DLL const Ceylan::LongFloat EpsilonFloat32 ;
00303
00304
00305
00311 extern CEYLAN_DLL const Ceylan::LongFloat EpsilonFloat64 ;
00312
00313
00314
00320 extern CEYLAN_DLL const Ceylan::LongFloat EpsilonLongFloat ;
00321
00322
00323
00329 extern CEYLAN_DLL const Ceylan::LongFloat Epsilon ;
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00347 CEYLAN_DLL bool IsNull( Ceylan::Float32 x ) ;
00348
00349
00355 CEYLAN_DLL bool IsNull( Ceylan::Float32 x, Ceylan::Float32 epsilon ) ;
00356
00357
00358
00364 CEYLAN_DLL bool IsNull( Ceylan::Float64 x ) ;
00365
00366
00367
00373 CEYLAN_DLL bool IsNull( Ceylan::Float64 x, Ceylan::Float64 epsilon ) ;
00374
00375
00376
00382 CEYLAN_DLL bool IsNull( Ceylan::LongFloat x ) ;
00383
00384
00385
00391 CEYLAN_DLL bool IsNull( Ceylan::LongFloat x,
00392 Ceylan::LongFloat epsilon ) ;
00393
00394
00395
00396
00397
00398
00399
00400
00406 CEYLAN_DLL bool AreEqual( Ceylan::Float32 x, Ceylan::Float32 y ) ;
00407
00408
00409
00415 CEYLAN_DLL bool AreEqual( Ceylan::Float32 x, Ceylan::Float32 y,
00416 Ceylan::Float32 epsilon ) ;
00417
00418
00419
00425 CEYLAN_DLL bool AreEqual( Ceylan::Float64 x, Ceylan::Float64 y ) ;
00426
00427
00428
00434 CEYLAN_DLL bool AreEqual( Ceylan::Float64 x, Ceylan::Float64 y,
00435 Ceylan::Float64 epsilon ) ;
00436
00437
00438
00450 CEYLAN_DLL bool AreExactlyEqual( Ceylan::Float64 x,
00451 Ceylan::Float64 y ) ;
00452
00453
00459 CEYLAN_DLL bool AreEqual( Ceylan::LongFloat x, Ceylan::LongFloat y ) ;
00460
00461
00462
00468 CEYLAN_DLL bool AreEqual( Ceylan::LongFloat x, Ceylan::LongFloat y,
00469 Ceylan::LongFloat epsilon ) ;
00470
00471
00472
00490 template <typename T>
00491 bool AreRelativelyEqual( T x, T y )
00492 {
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506 if ( IsNull( x + y ) )
00507 return IsNull( x ) ;
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523 return ( 2 * Abs( x - y ) / static_cast<T>( Abs( x + y ) )
00524 < static_cast<T>( EpsilonFloat32 ) ) ;
00525
00526 }
00527
00528
00529
00530
00546 template <typename T>
00547 bool AreRelativelyEqual( T x, T y, Ceylan::LongFloat epsilon )
00548 {
00549
00550 T castedEpsilon = static_cast<T>( epsilon ) ;
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560 if ( IsNull( x + y, castedEpsilon ) )
00561 return IsNull( x, castedEpsilon ) ;
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577 return ( 2 * Abs( x - y ) / static_cast<T>( Abs( x + y ) )
00578 < castedEpsilon ) ;
00579
00580 }
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00616 CEYLAN_DLL Ceylan::Float32 Floor( Ceylan::Float32 x ) ;
00617
00618
00619
00637 CEYLAN_DLL Ceylan::Float64 Floor( Ceylan::Float64 x ) ;
00638
00639
00640
00658 CEYLAN_DLL Ceylan::LongFloat Floor( Ceylan::LongFloat x ) ;
00659
00660
00661
00662
00680 CEYLAN_DLL Ceylan::Float32 Ceil( Ceylan::Float32 x ) ;
00681
00682
00683
00701 CEYLAN_DLL Ceylan::Float64 Ceil( Ceylan::Float64 x ) ;
00702
00703
00704
00722 CEYLAN_DLL Ceylan::LongFloat Ceil( Ceylan::LongFloat x ) ;
00723
00724
00725
00726
00727
00728
00729
00753 CEYLAN_DLL Ceylan::Float32 Round( Ceylan::Float32 x ) ;
00754
00755
00756
00791 CEYLAN_DLL Ceylan::Float32 Round( Ceylan::Float32 x,
00792 Ceylan::Uint8 precision ) ;
00793
00794
00795
00819 CEYLAN_DLL Ceylan::Float64 Round( Ceylan::Float64 x ) ;
00820
00821
00822
00859 CEYLAN_DLL Ceylan::Float64 Round( Ceylan::Float64 x,
00860 Ceylan::Uint8 precision ) ;
00861
00862
00863
00864
00888 CEYLAN_DLL Ceylan::LongFloat Round( Ceylan::LongFloat x ) ;
00889
00890
00891
00928 CEYLAN_DLL Ceylan::LongFloat Round( Ceylan::LongFloat x,
00929 Ceylan::Uint8 precision ) ;
00930
00931
00932
00933
00935
00936
00937
00938
00940 CEYLAN_DLL Ceylan::Sint8 Abs( Ceylan::Sint8 x ) ;
00941
00942
00944 CEYLAN_DLL Ceylan::Sint16 Abs( Ceylan::Sint16 x ) ;
00945
00946
00948 CEYLAN_DLL Ceylan::Sint32 Abs( Ceylan::Sint32 x ) ;
00949
00950
00952 CEYLAN_DLL Ceylan::SignedLongInteger Abs(
00953 Ceylan::SignedLongInteger x ) ;
00954
00955
00956
00963
00964
00965
00966
00967
00968
00969
00971 CEYLAN_DLL Ceylan::Float32 Abs( Ceylan::Float32 x ) ;
00972
00973
00975 CEYLAN_DLL Ceylan::Float64 Abs( Ceylan::Float64 x ) ;
00976
00977
00979 CEYLAN_DLL Ceylan::LongFloat Abs( Ceylan::LongFloat x ) ;
00980
00981
00982
00984
00985
00992 template<typename T>
00993 T Min( T x, T y )
00994 {
00995 return ( ( x < y ) ? x : y ) ;
00996
00997 }
00998
00999
01000
01009 template<typename T>
01010 T Min( T x, T y, T z )
01011 {
01012
01013 if ( x < y )
01014 return Min( x, z ) ;
01015 else
01016 return Min( y, z ) ;
01017
01018 }
01019
01020
01021
01022
01023
01024
01031 CEYLAN_DLL Ceylan::Sint8 Min( Ceylan::Sint8 x, Ceylan::Sint8 y ) ;
01032
01033
01034
01041 CEYLAN_DLL Ceylan::Uint8 Min( Ceylan::Uint8 x, Ceylan::Uint8 y ) ;
01042
01043
01044
01051 CEYLAN_DLL Ceylan::Sint16 Min( Ceylan::Sint16 x, Ceylan::Sint16 y ) ;
01052
01053
01054
01061 CEYLAN_DLL Ceylan::Uint16 Min( Ceylan::Uint16 x, Ceylan::Uint16 y ) ;
01062
01063
01064
01071 CEYLAN_DLL Ceylan::Sint32 Min( Ceylan::Sint32 x, Ceylan::Sint32 y ) ;
01072
01073
01074
01081 CEYLAN_DLL Ceylan::Uint32 Min( Ceylan::Uint32 x, Ceylan::Uint32 y ) ;
01082
01083
01084
01091 CEYLAN_DLL Ceylan::SignedLongInteger Min( Ceylan::SignedLongInteger x,
01092 Ceylan::SignedLongInteger y ) ;
01093
01094
01095
01102 CEYLAN_DLL Ceylan::UnsignedLongInteger Min(
01103 Ceylan::UnsignedLongInteger x,
01104 Ceylan::UnsignedLongInteger y ) ;
01105
01106
01107
01116
01117
01118
01119
01120
01121
01122
01123
01130 CEYLAN_DLL Ceylan::Float32 Min( Ceylan::Float32 x, Ceylan::Float32 y ) ;
01131
01132
01133
01140 CEYLAN_DLL Ceylan::Float64 Min( Ceylan::Float64 x, Ceylan::Float64 y ) ;
01141
01142
01143
01150 CEYLAN_DLL Ceylan::LongFloat Min( Ceylan::LongFloat x,
01151 Ceylan::LongFloat y ) ;
01152
01153
01154
01155
01156
01158
01159
01166 template<typename T>
01167 T Max( T x, T y )
01168 {
01169 return ( ( x > y ) ? x: y ) ;
01170
01171 }
01172
01173
01174
01175
01176
01177
01184 CEYLAN_DLL Ceylan::Sint8 Max( Ceylan::Sint8 x, Ceylan::Sint8 y ) ;
01185
01186
01187
01194 CEYLAN_DLL Ceylan::Uint8 Max( Ceylan::Uint8 x, Ceylan::Uint8 y ) ;
01195
01196
01197
01204 CEYLAN_DLL Ceylan::Sint16 Max( Ceylan::Sint16 x, Ceylan::Sint16 y ) ;
01205
01206
01207
01214 CEYLAN_DLL Ceylan::Uint16 Max( Ceylan::Uint16 x, Ceylan::Uint16 y ) ;
01215
01216
01217
01224 CEYLAN_DLL Ceylan::Sint32 Max( Ceylan::Sint32 x, Ceylan::Sint32 y ) ;
01225
01226
01227
01234 CEYLAN_DLL Ceylan::Uint32 Max( Ceylan::Uint32 x, Ceylan::Uint32 y ) ;
01235
01236
01237
01244 CEYLAN_DLL Ceylan::SignedLongInteger Max( Ceylan::SignedLongInteger x,
01245 Ceylan::SignedLongInteger y ) ;
01246
01247
01248
01255 CEYLAN_DLL Ceylan::UnsignedLongInteger Max(
01256 Ceylan::UnsignedLongInteger x,
01257 Ceylan::UnsignedLongInteger y ) ;
01258
01259
01260
01269
01270
01271
01272
01273
01274
01275
01276
01283 CEYLAN_DLL Ceylan::Float32 Max( Ceylan::Float32 x, Ceylan::Float32 y ) ;
01284
01285
01286
01293 CEYLAN_DLL Ceylan::Float64 Max( Ceylan::Float64 x, Ceylan::Float64 y ) ;
01294
01295
01296
01303 CEYLAN_DLL Ceylan::LongFloat Max( Ceylan::LongFloat x,
01304 Ceylan::LongFloat y ) ;
01305
01306
01307
01308
01309
01310
01311
01312
01318 CEYLAN_DLL Ceylan::Float32 Exp( Ceylan::Float32 x ) ;
01319
01320
01321
01327 CEYLAN_DLL Ceylan::Float64 Exp( Ceylan::Float64 x ) ;
01328
01329
01330
01336 CEYLAN_DLL Ceylan::LongFloat Exp( Ceylan::LongFloat x ) ;
01337
01338
01339
01340
01341
01342
01344 CEYLAN_DLL Ceylan::Float32 Pow( Ceylan::Float32 x, Ceylan::Float32 y ) ;
01345
01346
01348 CEYLAN_DLL Ceylan::Float64 Pow( Ceylan::Float64 x, Ceylan::Float64 y ) ;
01349
01350
01352 CEYLAN_DLL Ceylan::LongFloat Pow( Ceylan::LongFloat x,
01353 Ceylan::LongFloat y ) ;
01354
01355
01356
01357
01358
01359
01360
01362 CEYLAN_DLL Ceylan::Float32 Pow2( Ceylan::Float32 x ) ;
01363
01364
01366 CEYLAN_DLL Ceylan::Float64 Pow2( Ceylan::Float64 x ) ;
01367
01368
01370 CEYLAN_DLL Ceylan::LongFloat Pow2( Ceylan::LongFloat x ) ;
01371
01372
01373
01374
01375
01376
01377
01384 CEYLAN_DLL Ceylan::Float32 Log( Ceylan::Float32 x ) ;
01385
01386
01387
01394 CEYLAN_DLL Ceylan::Float64 Log( Ceylan::Float64 x ) ;
01395
01396
01397
01404 CEYLAN_DLL Ceylan::LongFloat Log( Ceylan::LongFloat x ) ;
01405
01406
01407
01408
01409
01410
01411
01418 CEYLAN_DLL Ceylan::Float32 Sqrt( Ceylan::Float32 x ) ;
01419
01420
01421
01428 CEYLAN_DLL Ceylan::Float64 Sqrt( Ceylan::Float64 x ) ;
01429
01430
01431
01438 CEYLAN_DLL Ceylan::LongFloat Sqrt( Ceylan::LongFloat x ) ;
01439
01440
01441
01442
01443
01444
01445
01446
01447
01448
01449
01450
01459 CEYLAN_DLL Ceylan::Float32 Cos( Ceylan::Float32 angle ) ;
01460
01461
01462
01469 CEYLAN_DLL Ceylan::Float64 Cos( Ceylan::Float64 angle ) ;
01470
01471
01472
01479 CEYLAN_DLL Ceylan::LongFloat Cos( Ceylan::LongFloat angle ) ;
01480
01481
01482
01483
01484
01485
01486
01487
01496 CEYLAN_DLL Ceylan::Float32 Sin( Ceylan::Float32 angle ) ;
01497
01498
01499
01506 CEYLAN_DLL Ceylan::Float64 Sin( Ceylan::Float64 angle ) ;
01507
01508
01509
01516 CEYLAN_DLL Ceylan::LongFloat Sin( Ceylan::LongFloat angle ) ;
01517
01518
01519
01520
01521
01522
01523
01524
01533 CEYLAN_DLL Ceylan::Float32 Tan( Ceylan::Float32 angle ) ;
01534
01535
01536
01543 CEYLAN_DLL Ceylan::Float64 Tan( Ceylan::Float64 angle ) ;
01544
01545
01546
01553 CEYLAN_DLL Ceylan::LongFloat Tan( Ceylan::LongFloat angle ) ;
01554
01555
01556
01557
01558 #if defined(CEYLAN_ARCH_NINTENDO_DS) && CEYLAN_ARCH_NINTENDO_DS == 1
01559
01560
01561
01562
01563
01568 CEYLAN_DLL Ceylan::Uint32 SqrtFixed( Ceylan::Uint32 x ) ;
01569
01570
01571
01572 #ifdef CEYLAN_RUNS_ON_ARM9
01573
01583 CEYLAN_DLL Ceylan::Sint32 CosFixed( Ceylan::Sint32 angle ) ;
01584
01585
01594 CEYLAN_DLL Ceylan::Sint32 SinFixed( Ceylan::Sint32 angle ) ;
01595
01596
01606 CEYLAN_DLL Ceylan::Sint32 TanFixed( Ceylan::Sint32 angle ) ;
01607
01608
01609 #endif // CEYLAN_RUNS_ON_ARM9
01610
01611
01612 #endif // defined(CEYLAN_ARCH_NINTENDO_DS) && CEYLAN_ARCH_NINTENDO_DS == 1
01613
01614
01615
01616
01622 CEYLAN_DLL AngleInRadians DegreeToRadian(
01623 AngleInDegrees angleInDegrees ) ;
01624
01625
01626
01642 CEYLAN_DLL Uint16 NextPowerOfTwo( Uint16 value ) ;
01643
01644
01645
01650 CEYLAN_DLL bool IsAPowerOfTwo( Uint16 value ) ;
01651
01652
01653
01654
01672 CEYLAN_DLL Uint16 NextMultipleOf( Uint16 multiple, Uint16 value ) ;
01673
01674
01675
01676
01677
01683 class CEYLAN_DLL IntToIntFunctor: public Ceylan::Functor
01684 {
01685
01686
01687 public:
01688
01689
01690
01692 explicit IntToIntFunctor(
01693 Ceylan::Sint32 creationParameter ) ;
01694
01695
01697 virtual ~IntToIntFunctor() throw() ;
01698
01699
01701 virtual Ceylan::Sint32 operator() (
01702 Ceylan::Sint32 callParameter ) = 0 ;
01703
01704
01705
01718 virtual const std::string toString(
01719 Ceylan::VerbosityLevels level = Ceylan::high ) const ;
01720
01721
01722
01723 protected:
01724
01725
01731 Ceylan::Sint32 _creationParameter ;
01732
01733
01734 } ;
01735
01736 }
01737
01738 }
01739
01740
01741
01742 #endif // CEYLAN_MATHS_BASIC_H_
01743