Skip to content

Commit d638133

Browse files
committed
Add more endpoints
1 parent a828a22 commit d638133

3 files changed

Lines changed: 199 additions & 0 deletions

File tree

Sources/CMathWrapper/MathWrapper.swift

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,113 @@ public func math_set_precision(_ precision: Int32) {
354354
public func math_get_precision() -> Int32 {
355355
return Int32(MathSettings.shared.precisionInt)
356356
}
357+
358+
// MARK: - Pronunciation
359+
360+
@_cdecl("math_spelled_out")
361+
public func math_spelled_out(_ math: UnsafeMutableRawPointer?, _ buffer: UnsafeMutablePointer<CChar>?, _ bufferSize: Int) -> Int32 {
362+
guard let box = toMathBox(math), let buffer = buffer, bufferSize > 0 else {
363+
return -4 // MATH_ERROR_NULL_POINTER
364+
}
365+
366+
let str = box.value.spelledOut
367+
guard let cString = str.cString(using: .utf8), cString.count <= bufferSize else {
368+
return -3 // MATH_ERROR_OUT_OF_RANGE
369+
}
370+
371+
cString.withUnsafeBufferPointer { ptr in
372+
buffer.update(from: ptr.baseAddress!, count: min(ptr.count, bufferSize))
373+
}
374+
375+
return 0 // MATH_SUCCESS
376+
}
377+
378+
@_cdecl("math_spelled_aviation")
379+
public func math_spelled_aviation(_ math: UnsafeMutableRawPointer?, _ buffer: UnsafeMutablePointer<CChar>?, _ bufferSize: Int) -> Int32 {
380+
guard let box = toMathBox(math), let buffer = buffer, bufferSize > 0 else {
381+
return -4 // MATH_ERROR_NULL_POINTER
382+
}
383+
384+
let str = box.value.spelledAviation
385+
guard let cString = str.cString(using: .utf8), cString.count <= bufferSize else {
386+
return -3 // MATH_ERROR_OUT_OF_RANGE
387+
}
388+
389+
cString.withUnsafeBufferPointer { ptr in
390+
buffer.update(from: ptr.baseAddress!, count: min(ptr.count, bufferSize))
391+
}
392+
393+
return 0 // MATH_SUCCESS
394+
}
395+
396+
// MARK: - Constants
397+
398+
@_cdecl("math_const_e")
399+
public func math_const_e() -> UnsafeMutableRawPointer? {
400+
return fromMath(MathConstants.e)
401+
}
402+
403+
@_cdecl("math_const_pi")
404+
public func math_const_pi() -> UnsafeMutableRawPointer? {
405+
return fromMath(MathConstants.pi)
406+
}
407+
408+
@_cdecl("math_const_tau")
409+
public func math_const_tau() -> UnsafeMutableRawPointer? {
410+
return fromMath(ExtendedMathConstants.tau)
411+
}
412+
413+
@_cdecl("math_const_phi")
414+
public func math_const_phi() -> UnsafeMutableRawPointer? {
415+
return fromMath(ExtendedMathConstants.φ)
416+
}
417+
418+
@_cdecl("math_const_sqrt2")
419+
public func math_const_sqrt2() -> UnsafeMutableRawPointer? {
420+
return fromMath(MathConstants.sqrt2)
421+
}
422+
423+
@_cdecl("math_const_sqrt3")
424+
public func math_const_sqrt3() -> UnsafeMutableRawPointer? {
425+
return fromMath(MathConstants.sqrt3)
426+
}
427+
428+
@_cdecl("math_const_speed_of_light")
429+
public func math_const_speed_of_light() -> UnsafeMutableRawPointer? {
430+
return fromMath(PhysicsConstants.c)
431+
}
432+
433+
@_cdecl("math_const_planck")
434+
public func math_const_planck() -> UnsafeMutableRawPointer? {
435+
return fromMath(PhysicsConstants.h)
436+
}
437+
438+
@_cdecl("math_const_gravitational")
439+
public func math_const_gravitational() -> UnsafeMutableRawPointer? {
440+
return fromMath(PhysicsConstants.G)
441+
}
442+
443+
@_cdecl("math_const_boltzmann")
444+
public func math_const_boltzmann() -> UnsafeMutableRawPointer? {
445+
return fromMath(PhysicsConstants.boltzmannConstant)
446+
}
447+
448+
@_cdecl("math_const_avogadro")
449+
public func math_const_avogadro() -> UnsafeMutableRawPointer? {
450+
return fromMath(PhysicsConstants.avogadroConstant)
451+
}
452+
453+
@_cdecl("math_const_electron_mass")
454+
public func math_const_electron_mass() -> UnsafeMutableRawPointer? {
455+
return fromMath(PhysicsConstants.electronMass)
456+
}
457+
458+
@_cdecl("math_const_proton_mass")
459+
public func math_const_proton_mass() -> UnsafeMutableRawPointer? {
460+
return fromMath(PhysicsConstants.protonMass)
461+
}
462+
463+
@_cdecl("math_const_elementary_charge")
464+
public func math_const_elementary_charge() -> UnsafeMutableRawPointer? {
465+
return fromMath(PhysicsConstants.elementaryCharge)
466+
}

Sources/CMathWrapper/include/PQMath.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,23 @@ class Math {
244244
return Math(math_previous_prime(ref_));
245245
}
246246

247+
// Pronunciation
248+
std::string spelledOut() const {
249+
char buffer[2048];
250+
if (math_spelled_out(ref_, buffer, sizeof(buffer)) != MATH_SUCCESS) {
251+
throw std::runtime_error("Failed to spell out number");
252+
}
253+
return std::string(buffer);
254+
}
255+
256+
std::string spelledAviation() const {
257+
char buffer[2048];
258+
if (math_spelled_aviation(ref_, buffer, sizeof(buffer)) != MATH_SUCCESS) {
259+
throw std::runtime_error("Failed to spell out number in aviation mode");
260+
}
261+
return std::string(buffer);
262+
}
263+
247264
// Friend function for stream output
248265
friend std::ostream& operator<<(std::ostream& os, const Math& m) {
249266
os << m.toString();
@@ -291,6 +308,34 @@ inline Math asin(const Math& value) { return value.asin(); }
291308
inline Math acos(const Math& value) { return value.acos(); }
292309
inline Math atan(const Math& value) { return value.atan(); }
293310

311+
// Constants namespace
312+
namespace Constants {
313+
// Math constants
314+
inline Math e() { return Math(math_const_e()); }
315+
inline Math pi() { return Math(math_const_pi()); }
316+
inline Math tau() { return Math(math_const_tau()); }
317+
inline Math phi() { return Math(math_const_phi()); } // Golden ratio
318+
inline Math sqrt2() { return Math(math_const_sqrt2()); }
319+
inline Math sqrt3() { return Math(math_const_sqrt3()); }
320+
321+
// Physics constants (SI units)
322+
inline Math speedOfLight() { return Math(math_const_speed_of_light()); } // m/s
323+
inline Math planck() { return Math(math_const_planck()); } // J·s
324+
inline Math gravitational() { return Math(math_const_gravitational()); } // m³/(kg·s²)
325+
inline Math boltzmann() { return Math(math_const_boltzmann()); } // J/K
326+
inline Math avogadro() { return Math(math_const_avogadro()); } // mol⁻¹
327+
inline Math electronMass() { return Math(math_const_electron_mass()); } // kg
328+
inline Math protonMass() { return Math(math_const_proton_mass()); } // kg
329+
inline Math elementaryCharge() { return Math(math_const_elementary_charge()); } // C
330+
331+
// Aliases
332+
inline Math c() { return speedOfLight(); }
333+
inline Math h() { return planck(); }
334+
inline Math G() { return gravitational(); }
335+
inline Math k() { return boltzmann(); }
336+
inline Math NA() { return avogadro(); }
337+
}
338+
294339
} // namespace PQMath
295340

296341
#endif /* PQMATH_HPP */

Sources/CMathWrapper/include/math_wrapper.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,50 @@ void math_set_precision(int precision);
363363
*/
364364
int math_get_precision(void);
365365

366+
/* ============================================================================
367+
* MARK: - Pronunciation
368+
* ========================================================================= */
369+
370+
/**
371+
* Spells out a number in English.
372+
* @param math The Math object
373+
* @param buffer Buffer to store the spelled-out text
374+
* @param buffer_size Size of the buffer
375+
* @return MATH_SUCCESS or error code
376+
*/
377+
MathError math_spelled_out(MathRef math, char* buffer, size_t buffer_size);
378+
379+
/**
380+
* Spells out a number in aviation pronunciation.
381+
* @param math The Math object
382+
* @param buffer Buffer to store the spelled-out text
383+
* @param buffer_size Size of the buffer
384+
* @return MATH_SUCCESS or error code
385+
*/
386+
MathError math_spelled_aviation(MathRef math, char* buffer, size_t buffer_size);
387+
388+
/* ============================================================================
389+
* MARK: - Constants
390+
* ========================================================================= */
391+
392+
/* Math Constants */
393+
MathRef math_const_e(void); /* Euler's number (≈2.718) */
394+
MathRef math_const_pi(void); /* Pi (≈3.14159) */
395+
MathRef math_const_tau(void); /* Tau (2π ≈ 6.283) */
396+
MathRef math_const_phi(void); /* Golden ratio (≈1.618) */
397+
MathRef math_const_sqrt2(void); /* √2 (≈1.414) */
398+
MathRef math_const_sqrt3(void); /* √3 (≈1.732) */
399+
400+
/* Physics Constants (values in SI units) */
401+
MathRef math_const_speed_of_light(void); /* c ≈ 299792458 m/s */
402+
MathRef math_const_planck(void); /* h ≈ 6.626×10⁻³⁴ J·s */
403+
MathRef math_const_gravitational(void); /* G ≈ 6.674×10⁻¹¹ m³/(kg·s²) */
404+
MathRef math_const_boltzmann(void); /* k ≈ 1.381×10⁻²³ J/K */
405+
MathRef math_const_avogadro(void); /* N_A ≈ 6.022×10²³ mol⁻¹ */
406+
MathRef math_const_electron_mass(void); /* m_e ≈ 9.109×10⁻³¹ kg */
407+
MathRef math_const_proton_mass(void); /* m_p ≈ 1.673×10⁻²⁷ kg */
408+
MathRef math_const_elementary_charge(void); /* e ≈ 1.602×10⁻¹⁹ C */
409+
366410
#ifdef __cplusplus
367411
}
368412
#endif

0 commit comments

Comments
 (0)