Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 3baf959

Browse files
committed
math: Add f64 powf and powi
1 parent 2221e5d commit 3baf959

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

libraries/math/src/instruction.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ pub enum MathInstruction {
7070
/// The divisor
7171
divisor: f32,
7272
},
73+
/// Pow two float values
74+
///
75+
/// No accounts required for this instruction
76+
F64Pow {
77+
/// The base
78+
base: f64,
79+
/// The exponent
80+
exponent: f64,
81+
},
7382
/// Don't do anything for comparison
7483
///
7584
/// No accounts required for this instruction
@@ -159,6 +168,17 @@ pub fn f32_divide(dividend: f32, divisor: f32) -> Instruction {
159168
}
160169
}
161170

171+
/// Create F64Pow instruction
172+
pub fn f64_pow(base: f64, exponent: f64) -> Instruction {
173+
Instruction {
174+
program_id: id(),
175+
accounts: vec![],
176+
data: MathInstruction::F64Pow { base, exponent }
177+
.try_to_vec()
178+
.unwrap(),
179+
}
180+
}
181+
162182
/// Create PreciseSquareRoot instruction
163183
pub fn noop() -> Instruction {
164184
Instruction {

libraries/math/src/processor.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ pub fn process_instruction(
104104
msg!("{}", result as u64);
105105
Ok(())
106106
}
107+
MathInstruction::F64Pow { base, exponent } => {
108+
msg!("Calculating f64 Pow");
109+
sol_log_compute_units();
110+
let _result = base.powi(5); // ok
111+
let result = base.powf(exponent); // symbol not found
112+
sol_log_compute_units();
113+
msg!("{}", result as u64);
114+
Ok(())
115+
}
107116
MathInstruction::Noop => {
108117
msg!("Do nothing");
109118
msg!("{}", 0_u64);

libraries/math/tests/instruction_count.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,22 @@ async fn test_f32_divide() {
148148
banks_client.process_transaction(transaction).await.unwrap();
149149
}
150150

151+
#[tokio::test]
152+
async fn test_f64_pow() {
153+
let mut pc = ProgramTest::new("spl_math", id(), processor!(process_instruction));
154+
155+
pc.set_bpf_compute_max_units(30_000);
156+
157+
let (mut banks_client, payer, recent_blockhash) = pc.start().await;
158+
159+
let mut transaction = Transaction::new_with_payer(
160+
&[instruction::f64_pow(50_f64, 10.5_f64)],
161+
Some(&payer.pubkey()),
162+
);
163+
transaction.sign(&[&payer], recent_blockhash);
164+
banks_client.process_transaction(transaction).await.unwrap();
165+
}
166+
151167
#[tokio::test]
152168
async fn test_noop() {
153169
let mut pc = ProgramTest::new("spl_math", id(), processor!(process_instruction));

0 commit comments

Comments
 (0)