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
00489 template <typename T> bool AreRelativelyEqual( T x, T y )
00490 {
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504 if ( IsNull( x + y ) )
00505 return IsNull( x ) ;
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 return ( Abs( x - y ) / ( x + y ) < EpsilonFloat32 ) ;
00517
00518 }
00519
00520
00521
00522
00537 template <typename T> bool AreRelativelyEqual( T x, T y,
00538 Ceylan::LongFloat epsilon )
00539 {
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550 if ( IsNull( x + y, epsilon ) )
00551 return IsNull( x, epsilon ) ;
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562 return ( Abs( x - y ) / ( x + y ) < epsilon ) ;
00563
00564 }
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00600 CEYLAN_DLL Ceylan::Float32 Floor( Ceylan::Float32 x ) ;
00601
00602
00603
00621 CEYLAN_DLL Ceylan::Float64 Floor( Ceylan::Float64 x ) ;
00622
00623
00624
00642 CEYLAN_DLL Ceylan::LongFloat Floor( Ceylan::LongFloat x ) ;
00643
00644
00645
00646
00664 CEYLAN_DLL Ceylan::Float32 Ceil( Ceylan::Float32 x ) ;
00665
00666
00667
00685 CEYLAN_DLL Ceylan::Float64 Ceil( Ceylan::Float64 x ) ;
00686
00687
00688
00706 CEYLAN_DLL Ceylan::LongFloat Ceil( Ceylan::LongFloat x ) ;
00707
00708
00709
00710
00711
00712
00713
00737 CEYLAN_DLL Ceylan::Float32 Round( Ceylan::Float32 x ) ;
00738
00739
00740
00775 CEYLAN_DLL Ceylan::Float32 Round( Ceylan::Float32 x,
00776 Ceylan::Uint8 precision ) ;
00777
00778
00779
00803 CEYLAN_DLL Ceylan::Float64 Round( Ceylan::Float64 x ) ;
00804
00805
00806
00843 CEYLAN_DLL Ceylan::Float64 Round( Ceylan::Float64 x,
00844 Ceylan::Uint8 precision ) ;
00845
00846
00847
00848
00872 CEYLAN_DLL Ceylan::LongFloat Round( Ceylan::LongFloat x ) ;
00873
00874
00875
00912 CEYLAN_DLL Ceylan::LongFloat Round( Ceylan::LongFloat x,
00913 Ceylan::Uint8 precision ) ;
00914
00915
00916
00917
00919
00920
00921
00922
00924 CEYLAN_DLL Ceylan::Sint8 Abs( Ceylan::Sint8 x ) ;
00925
00926
00928 CEYLAN_DLL Ceylan::Sint16 Abs( Ceylan::Sint16 x ) ;
00929
00930
00932 CEYLAN_DLL Ceylan::Sint32 Abs( Ceylan::Sint32 x ) ;
00933
00934
00936 CEYLAN_DLL Ceylan::SignedLongInteger Abs(
00937 Ceylan::SignedLongInteger x ) ;
00938
00939
00940
00947
00948
00949
00950
00951
00952
00953
00955 CEYLAN_DLL Ceylan::Float32 Abs( Ceylan::Float32 x ) ;
00956
00957
00959 CEYLAN_DLL Ceylan::Float64 Abs( Ceylan::Float64 x ) ;
00960
00961
00963 CEYLAN_DLL Ceylan::LongFloat Abs( Ceylan::LongFloat x ) ;
00964
00965
00966
00968
00969
00976 template<typename T>
00977 T Min( T x, T y )
00978 {
00979 return ( ( x < y ) ? x : y ) ;
00980
00981 }
00982
00983
00984
00993 template<typename T>
00994 T Min( T x, T y, T z )
00995 {
00996
00997 if ( x < y )
00998 return Min( x, z ) ;
00999 else
01000 return Min( y, z ) ;
01001
01002 }
01003
01004
01005
01006
01007
01008
01015 CEYLAN_DLL Ceylan::Sint8 Min( Ceylan::Sint8 x, Ceylan::Sint8 y ) ;
01016
01017
01018
01025 CEYLAN_DLL Ceylan::Uint8 Min( Ceylan::Uint8 x, Ceylan::Uint8 y ) ;
01026
01027
01028
01035 CEYLAN_DLL Ceylan::Sint16 Min( Ceylan::Sint16 x, Ceylan::Sint16 y ) ;
01036
01037
01038
01045 CEYLAN_DLL Ceylan::Uint16 Min( Ceylan::Uint16 x, Ceylan::Uint16 y ) ;
01046
01047
01048
01055 CEYLAN_DLL Ceylan::Sint32 Min( Ceylan::Sint32 x, Ceylan::Sint32 y ) ;
01056
01057
01058
01065 CEYLAN_DLL Ceylan::Uint32 Min( Ceylan::Uint32 x, Ceylan::Uint32 y ) ;
01066
01067
01068
01075 CEYLAN_DLL Ceylan::SignedLongInteger Min( Ceylan::SignedLongInteger x,
01076 Ceylan::SignedLongInteger y ) ;
01077
01078
01079
01086 CEYLAN_DLL Ceylan::UnsignedLongInteger Min(
01087 Ceylan::UnsignedLongInteger x,
01088 Ceylan::UnsignedLongInteger y ) ;
01089
01090
01091
01100
01101
01102
01103
01104
01105
01106
01107
01114 CEYLAN_DLL Ceylan::Float32 Min( Ceylan::Float32 x, Ceylan::Float32 y ) ;
01115
01116
01117
01124 CEYLAN_DLL Ceylan::Float64 Min( Ceylan::Float64 x, Ceylan::Float64 y ) ;
01125
01126
01127
01134 CEYLAN_DLL Ceylan::LongFloat Min( Ceylan::LongFloat x,
01135 Ceylan::LongFloat y ) ;
01136
01137
01138
01139
01140
01142
01143
01150 template<typename T>
01151 T Max( T x, T y )
01152 {
01153 return ( ( x > y ) ? x: y ) ;
01154
01155 }
01156
01157
01158
01159
01160
01161
01168 CEYLAN_DLL Ceylan::Sint8 Max( Ceylan::Sint8 x, Ceylan::Sint8 y ) ;
01169
01170
01171
01178 CEYLAN_DLL Ceylan::Uint8 Max( Ceylan::Uint8 x, Ceylan::Uint8 y ) ;
01179
01180
01181
01188 CEYLAN_DLL Ceylan::Sint16 Max( Ceylan::Sint16 x, Ceylan::Sint16 y ) ;
01189
01190
01191
01198 CEYLAN_DLL Ceylan::Uint16 Max( Ceylan::Uint16 x, Ceylan::Uint16 y ) ;
01199
01200
01201
01208 CEYLAN_DLL Ceylan::Sint32 Max( Ceylan::Sint32 x, Ceylan::Sint32 y ) ;
01209
01210
01211
01218 CEYLAN_DLL Ceylan::Uint32 Max( Ceylan::Uint32 x, Ceylan::Uint32 y ) ;
01219
01220
01221
01228 CEYLAN_DLL Ceylan::SignedLongInteger Max( Ceylan::SignedLongInteger x,
01229 Ceylan::SignedLongInteger y ) ;
01230
01231
01232
01239 CEYLAN_DLL Ceylan::UnsignedLongInteger Max(
01240 Ceylan::UnsignedLongInteger x,
01241 Ceylan::UnsignedLongInteger y ) ;
01242
01243
01244
01253
01254
01255
01256
01257
01258
01259
01260
01267 CEYLAN_DLL Ceylan::Float32 Max( Ceylan::Float32 x, Ceylan::Float32 y ) ;
01268
01269
01270
01277 CEYLAN_DLL Ceylan::Float64 Max( Ceylan::Float64 x, Ceylan::Float64 y ) ;
01278
01279
01280
01287 CEYLAN_DLL Ceylan::LongFloat Max( Ceylan::LongFloat x,
01288 Ceylan::LongFloat y ) ;
01289
01290
01291
01292
01293
01294
01295
01296
01302 CEYLAN_DLL Ceylan::Float32 Exp( Ceylan::Float32 x ) ;
01303
01304
01305
01311 CEYLAN_DLL Ceylan::Float64 Exp( Ceylan::Float64 x ) ;
01312
01313
01314
01320 CEYLAN_DLL Ceylan::LongFloat Exp( Ceylan::LongFloat x ) ;
01321
01322
01323
01324
01325
01326
01328 CEYLAN_DLL Ceylan::Float32 Pow( Ceylan::Float32 x, Ceylan::Float32 y ) ;
01329
01330
01332 CEYLAN_DLL Ceylan::Float64 Pow( Ceylan::Float64 x, Ceylan::Float64 y ) ;
01333
01334
01336 CEYLAN_DLL Ceylan::LongFloat Pow( Ceylan::LongFloat x,
01337 Ceylan::LongFloat y ) ;
01338
01339
01340
01341
01342
01343
01344
01346 CEYLAN_DLL Ceylan::Float32 Pow2( Ceylan::Float32 x ) ;
01347
01348
01350 CEYLAN_DLL Ceylan::Float64 Pow2( Ceylan::Float64 x ) ;
01351
01352
01354 CEYLAN_DLL Ceylan::LongFloat Pow2( Ceylan::LongFloat x ) ;
01355
01356
01357
01358
01359
01360
01361
01368 CEYLAN_DLL Ceylan::Float32 Log( Ceylan::Float32 x ) ;
01369
01370
01371
01378 CEYLAN_DLL Ceylan::Float64 Log( Ceylan::Float64 x ) ;
01379
01380
01381
01388 CEYLAN_DLL Ceylan::LongFloat Log( Ceylan::LongFloat x ) ;
01389
01390
01391
01392
01393
01394
01395
01402 CEYLAN_DLL Ceylan::Float32 Sqrt( Ceylan::Float32 x ) ;
01403
01404
01405
01412 CEYLAN_DLL Ceylan::Float64 Sqrt( Ceylan::Float64 x ) ;
01413
01414
01415
01422 CEYLAN_DLL Ceylan::LongFloat Sqrt( Ceylan::LongFloat x ) ;
01423
01424
01425
01426
01427
01428
01429
01430
01431
01432
01433
01434
01443 CEYLAN_DLL Ceylan::Float32 Cos( Ceylan::Float32 angle ) ;
01444
01445
01446
01453 CEYLAN_DLL Ceylan::Float64 Cos( Ceylan::Float64 angle ) ;
01454
01455
01456
01463 CEYLAN_DLL Ceylan::LongFloat Cos( Ceylan::LongFloat angle ) ;
01464
01465
01466
01467
01468
01469
01470
01471
01480 CEYLAN_DLL Ceylan::Float32 Sin( Ceylan::Float32 angle ) ;
01481
01482
01483
01490 CEYLAN_DLL Ceylan::Float64 Sin( Ceylan::Float64 angle ) ;
01491
01492
01493
01500 CEYLAN_DLL Ceylan::LongFloat Sin( Ceylan::LongFloat angle ) ;
01501
01502
01503
01504
01505
01506
01507
01508
01517 CEYLAN_DLL Ceylan::Float32 Tan( Ceylan::Float32 angle ) ;
01518
01519
01520
01527 CEYLAN_DLL Ceylan::Float64 Tan( Ceylan::Float64 angle ) ;
01528
01529
01530
01537 CEYLAN_DLL Ceylan::LongFloat Tan( Ceylan::LongFloat angle ) ;
01538
01539
01540
01541
01542 #if defined(CEYLAN_ARCH_NINTENDO_DS) && CEYLAN_ARCH_NINTENDO_DS == 1
01543
01544
01545
01546
01547
01552 CEYLAN_DLL Ceylan::Uint32 SqrtFixed( Ceylan::Uint32 x ) ;
01553
01554
01555
01556 #ifdef CEYLAN_RUNS_ON_ARM9
01557
01567 CEYLAN_DLL Ceylan::Sint32 CosFixed( Ceylan::Sint32 angle ) ;
01568
01569
01578 CEYLAN_DLL Ceylan::Sint32 SinFixed( Ceylan::Sint32 angle ) ;
01579
01580
01590 CEYLAN_DLL Ceylan::Sint32 TanFixed( Ceylan::Sint32 angle ) ;
01591
01592
01593 #endif // CEYLAN_RUNS_ON_ARM9
01594
01595
01596 #endif // defined(CEYLAN_ARCH_NINTENDO_DS) && CEYLAN_ARCH_NINTENDO_DS == 1
01597
01598
01599
01600
01606 CEYLAN_DLL AngleInRadians DegreeToRadian(
01607 AngleInDegrees angleInDegrees ) ;
01608
01609
01610
01626 CEYLAN_DLL Uint16 NextPowerOfTwo( Uint16 value ) ;
01627
01628
01629
01634 CEYLAN_DLL bool IsAPowerOfTwo( Uint16 value ) ;
01635
01636
01637
01638
01656 CEYLAN_DLL Uint16 NextMultipleOf( Uint16 multiple, Uint16 value ) ;
01657
01658
01659
01660
01661
01667 class CEYLAN_DLL IntToIntFunctor: public Ceylan::Functor
01668 {
01669
01670
01671 public:
01672
01673
01674
01676 explicit IntToIntFunctor(
01677 Ceylan::Sint32 creationParameter ) ;
01678
01679
01681 virtual ~IntToIntFunctor() throw() ;
01682
01683
01685 virtual Ceylan::Sint32 operator() (
01686 Ceylan::Sint32 callParameter ) = 0 ;
01687
01688
01689
01702 virtual const std::string toString(
01703 Ceylan::VerbosityLevels level = Ceylan::high ) const ;
01704
01705
01706
01707 protected:
01708
01709
01715 Ceylan::Sint32 _creationParameter ;
01716
01717
01718 } ;
01719
01720 }
01721
01722 }
01723
01724
01725
01726 #endif // CEYLAN_MATHS_BASIC_H_
01727